Whamcloud - gitweb
LU-1816 scrub: OI scrub skips new created objects for once
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_handler.c
37  *
38  * Top-level entry points into osd module
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  *         Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <linux/module.h>
47
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/fs.h>
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
56 /* simple_mkdir() */
57 #include <lvfs.h>
58
59 /*
60  * struct OBD_{ALLOC,FREE}*()
61  * OBD_FAIL_CHECK
62  */
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66
67 /* fid_is_local() */
68 #include <lustre_fid.h>
69
70 #include "osd_internal.h"
71 #include "osd_igif.h"
72
73 /* llo_* api support */
74 #include <md_object.h>
75 /* dt_acct_features */
76 #include <lquota.h>
77
78 #ifdef HAVE_LDISKFS_PDO
79 int ldiskfs_pdo = 1;
80 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
81                 "ldiskfs with parallel directory operations");
82 #else
83 int ldiskfs_pdo = 0;
84 #endif
85
86 static const char dot[] = ".";
87 static const char dotdot[] = "..";
88 static const char remote_obj_dir[] = "REM_OBJ_DIR";
89
90 static const struct lu_object_operations      osd_lu_obj_ops;
91 static const struct dt_object_operations      osd_obj_ops;
92 static const struct dt_object_operations      osd_obj_ea_ops;
93 static const struct dt_object_operations      osd_obj_otable_it_ops;
94 static const struct dt_index_operations       osd_index_iam_ops;
95 static const struct dt_index_operations       osd_index_ea_ops;
96
97 static int osd_has_index(const struct osd_object *obj)
98 {
99         return obj->oo_dt.do_index_ops != NULL;
100 }
101
102 static int osd_object_invariant(const struct lu_object *l)
103 {
104         return osd_invariant(osd_obj(l));
105 }
106
107 #ifdef HAVE_QUOTA_SUPPORT
108 static inline void
109 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save, bool is_md)
110 {
111         struct md_ucred *uc;
112         struct cred     *tc;
113
114         if (!is_md)
115                 /* OFD support */
116                 return;
117
118         uc = md_ucred(env);
119
120         LASSERT(uc != NULL);
121
122         save->oc_uid = current_fsuid();
123         save->oc_gid = current_fsgid();
124         save->oc_cap = current_cap();
125         if ((tc = prepare_creds())) {
126                 tc->fsuid         = uc->mu_fsuid;
127                 tc->fsgid         = uc->mu_fsgid;
128                 commit_creds(tc);
129         }
130         /* XXX not suboptimal */
131         cfs_curproc_cap_unpack(uc->mu_cap);
132 }
133
134 static inline void
135 osd_pop_ctxt(struct osd_ctxt *save, bool is_md)
136 {
137         struct cred *tc;
138
139         if (!is_md)
140                 /* OFD support */
141                 return;
142
143         if ((tc = prepare_creds())) {
144                 tc->fsuid         = save->oc_uid;
145                 tc->fsgid         = save->oc_gid;
146                 tc->cap_effective = save->oc_cap;
147                 commit_creds(tc);
148         }
149 }
150 #endif
151
152 /*
153  * Concurrency: doesn't matter
154  */
155 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
156 {
157         return osd_oti_get(env)->oti_r_locks > 0;
158 }
159
160 /*
161  * Concurrency: doesn't matter
162  */
163 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
164 {
165         struct osd_thread_info *oti = osd_oti_get(env);
166         return oti->oti_w_locks > 0 && o->oo_owner == env;
167 }
168
169 /*
170  * Concurrency: doesn't access mutable data
171  */
172 static int osd_root_get(const struct lu_env *env,
173                         struct dt_device *dev, struct lu_fid *f)
174 {
175         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
176         return 0;
177 }
178
179 static inline int osd_qid_type(struct osd_thandle *oh, int i)
180 {
181         return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
182 }
183
184 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
185 {
186         oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
187 }
188
189 void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
190                      int type, uid_t id, struct inode *inode)
191 {
192 #ifdef CONFIG_QUOTA
193         int i, allocated = 0;
194         struct osd_object *obj;
195
196         LASSERT(dt != NULL);
197         LASSERT(oh != NULL);
198         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
199                  oh->ot_id_cnt);
200
201         /* id entry is allocated in the quota file */
202         if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
203                 allocated = 1;
204
205         for (i = 0; i < oh->ot_id_cnt; i++) {
206                 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
207                         return;
208         }
209
210         if (unlikely(i >= OSD_MAX_UGID_CNT)) {
211                 CERROR("more than %d uid/gids for a transaction?\n", i);
212                 return;
213         }
214
215         oh->ot_id_array[i] = id;
216         osd_qid_set_type(oh, i, type);
217         oh->ot_id_cnt++;
218         obj = osd_dt_obj(dt);
219         oh->ot_credits += (allocated || id == 0) ?
220                 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
221 #endif
222 }
223
224 /*
225  * OSD object methods.
226  */
227
228 /*
229  * Concurrency: no concurrent access is possible that early in object
230  * life-cycle.
231  */
232 static struct lu_object *osd_object_alloc(const struct lu_env *env,
233                                           const struct lu_object_header *hdr,
234                                           struct lu_device *d)
235 {
236         struct osd_object *mo;
237
238         OBD_ALLOC_PTR(mo);
239         if (mo != NULL) {
240                 struct lu_object *l;
241
242                 l = &mo->oo_dt.do_lu;
243                 dt_object_init(&mo->oo_dt, NULL, d);
244                 if (osd_dev(d)->od_iop_mode)
245                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
246                 else
247                         mo->oo_dt.do_ops = &osd_obj_ops;
248
249                 l->lo_ops = &osd_lu_obj_ops;
250                 cfs_init_rwsem(&mo->oo_sem);
251                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
252                 cfs_spin_lock_init(&mo->oo_guard);
253                 return l;
254         } else {
255                 return NULL;
256         }
257 }
258
259 static int osd_get_lma(struct inode *inode, struct dentry *dentry,
260                        struct lustre_mdt_attrs *lma)
261 {
262         int rc;
263
264         dentry->d_inode = inode;
265         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
266                                    sizeof(*lma));
267         if (rc > 0) {
268                 /* Check LMA compatibility */
269                 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
270                         CWARN("%.16s: unsupported incompat LMA feature(s) "
271                               "%lx/%#x\n",
272                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
273                               inode->i_ino, le32_to_cpu(lma->lma_incompat) &
274                                                         ~LMA_INCOMPAT_SUPP);
275                         rc = -ENOSYS;
276                 } else {
277                         lustre_lma_swab(lma);
278                         rc = 0;
279                 }
280         } else if (rc == 0) {
281                 rc = -ENODATA;
282         }
283
284         return rc;
285 }
286
287 /*
288  * retrieve object from backend ext fs.
289  **/
290 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
291                        struct osd_inode_id *id)
292 {
293         struct inode *inode = NULL;
294
295         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
296         if (IS_ERR(inode)) {
297                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
298                        id->oii_ino, PTR_ERR(inode));
299         } else if (id->oii_gen != OSD_OII_NOGEN &&
300                    inode->i_generation != id->oii_gen) {
301                 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
302                        "gen1 = %u\n",
303                        id->oii_ino, id->oii_gen, inode->i_generation);
304                 iput(inode);
305                 inode = ERR_PTR(-ESTALE);
306         } else if (inode->i_nlink == 0) {
307                 /* due to parallel readdir and unlink,
308                 * we can have dead inode here. */
309                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
310                 make_bad_inode(inode);
311                 iput(inode);
312                 inode = ERR_PTR(-ESTALE);
313         } else if (is_bad_inode(inode)) {
314                 CWARN("%.16s: bad inode: ino = %u\n",
315                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
316                 iput(inode);
317                 inode = ERR_PTR(-ENOENT);
318         } else {
319                 if (id->oii_gen == OSD_OII_NOGEN)
320                         osd_id_gen(id, inode->i_ino, inode->i_generation);
321
322                 /* Do not update file c/mtime in ldiskfs.
323                  * NB: we don't have any lock to protect this because we don't
324                  * have reference on osd_object now, but contention with
325                  * another lookup + attr_set can't happen in the tiny window
326                  * between if (...) and set S_NOCMTIME. */
327                 if (!(inode->i_flags & S_NOCMTIME))
328                         inode->i_flags |= S_NOCMTIME;
329         }
330         return inode;
331 }
332
333 struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
334                            struct osd_inode_id *id, struct lu_fid *fid)
335 {
336         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
337         struct inode            *inode;
338         int                      rc;
339
340         inode = osd_iget(info, dev, id);
341         if (IS_ERR(inode))
342                 return inode;
343
344         rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
345         if (rc == 0) {
346                 *fid = lma->lma_self_fid;
347         } else if (rc == -ENODATA) {
348                 LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
349         } else {
350                 iput(inode);
351                 inode = ERR_PTR(rc);
352         }
353         return inode;
354 }
355
356 static struct inode *
357 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
358                 struct osd_inode_id *id, const struct lu_fid *fid)
359 {
360         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
361         struct inode            *inode;
362         int                      rc;
363
364         inode = osd_iget(info, dev, id);
365         if (IS_ERR(inode))
366                 return inode;
367
368         rc = osd_get_lma(inode, &info->oti_obj_dentry, lma);
369         if (rc != 0) {
370                 if (rc == -ENODATA) {
371                         CDEBUG(D_LFSCK, "inconsistent obj: NULL, %lu, "DFID"\n",
372                                inode->i_ino, PFID(fid));
373                         rc = -EREMCHG;
374                 }
375                 iput(inode);
376                 return ERR_PTR(rc);
377         }
378
379         if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
380                 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
381                        PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
382                 iput(inode);
383                 return ERR_PTR(EREMCHG);
384         }
385         return inode;
386 }
387
388 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
389                           const struct lu_fid *fid,
390                           const struct lu_object_conf *conf)
391 {
392         struct osd_thread_info *info;
393         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
394         struct osd_device      *dev;
395         struct osd_idmap_cache *oic;
396         struct osd_inode_id    *id;
397         struct inode           *inode;
398         struct osd_scrub       *scrub;
399         struct scrub_file      *sf;
400         int                     result;
401         int                     verify = 0;
402         ENTRY;
403
404         LINVRNT(osd_invariant(obj));
405         LASSERT(obj->oo_inode == NULL);
406         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
407
408         dev = osd_dev(ldev);
409         scrub = &dev->od_scrub;
410         sf = &scrub->os_file;
411         info = osd_oti_get(env);
412         LASSERT(info);
413         oic = &info->oti_cache;
414         id  = &oic->oic_lid;
415
416         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
417                 RETURN(-ENOENT);
418
419         if (fid_is_norm(fid)) {
420                 /* Search order: 1. per-thread cache. */
421                 if (lu_fid_eq(fid, &oic->oic_fid)) {
422                         goto iget;
423                 } else if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
424                         /* Search order: 2. OI scrub pending list. */
425                         result = osd_oii_lookup(dev, fid, id);
426                         if (result == 0)
427                                 goto iget;
428                 }
429
430                 if (sf->sf_flags & SF_INCONSISTENT)
431                         verify = 1;
432         }
433
434         /*
435          * Objects are created as locking anchors or place holders for objects
436          * yet to be created. No need to osd_oi_lookup() at here because FID
437          * shouldn't never be re-used, if it's really a duplicate FID from
438          * unexpected reason, we should be able to detect it later by calling
439          * do_create->osd_oi_insert()
440          */
441         if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
442                 GOTO(out, result = 0);
443
444         /* Search order: 3. OI files. */
445         result = osd_oi_lookup(info, dev, fid, id);
446         if (result == -ENOENT) {
447                 if (!fid_is_norm(fid) ||
448                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
449                                       sf->sf_oi_bitmap))
450                         GOTO(out, result = 0);
451
452                 goto trigger;
453         }
454
455         if (result != 0)
456                 GOTO(out, result);
457
458 iget:
459         if (verify == 0)
460                 inode = osd_iget(info, dev, id);
461         else
462                 inode = osd_iget_verify(info, dev, id, fid);
463         if (IS_ERR(inode)) {
464                 result = PTR_ERR(inode);
465                 if (result == -ENOENT || result == -ESTALE) {
466                         fid_zero(&oic->oic_fid);
467                         result = 0;
468                 } else if (result == -EREMCHG) {
469
470 trigger:
471                         if (thread_is_running(&scrub->os_thread)) {
472                                 result = -EINPROGRESS;
473                         } else if (!scrub->os_no_scrub) {
474                                 result = osd_scrub_start(dev);
475                                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
476                                                "for "DFID", rc = %d [1]\n",
477                                                LDISKFS_SB(osd_sb(dev))->s_es->\
478                                                s_volume_name,PFID(fid), result);
479                                 if (result == 0 || result == -EALREADY)
480                                         result = -EINPROGRESS;
481                                 else
482                                         result = -EREMCHG;
483                         }
484                 }
485
486                 GOTO(out, result);
487         }
488
489         obj->oo_inode = inode;
490         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
491         if (dev->od_iop_mode) {
492                 obj->oo_compat_dot_created = 1;
493                 obj->oo_compat_dotdot_created = 1;
494         }
495
496         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
497                 GOTO(out, result = 0);
498
499         LASSERT(obj->oo_hl_head == NULL);
500         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
501         if (obj->oo_hl_head == NULL) {
502                 obj->oo_inode = NULL;
503                 iput(inode);
504                 GOTO(out, result = -ENOMEM);
505         }
506         GOTO(out, result = 0);
507
508 out:
509         LINVRNT(osd_invariant(obj));
510         return result;
511 }
512
513 /*
514  * Concurrency: shouldn't matter.
515  */
516 static void osd_object_init0(struct osd_object *obj)
517 {
518         LASSERT(obj->oo_inode != NULL);
519         obj->oo_dt.do_body_ops = &osd_body_ops;
520         obj->oo_dt.do_lu.lo_header->loh_attr |=
521                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
522 }
523
524 /*
525  * Concurrency: no concurrent access is possible that early in object
526  * life-cycle.
527  */
528 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
529                            const struct lu_object_conf *conf)
530 {
531         struct osd_object *obj = osd_obj(l);
532         int result;
533
534         LINVRNT(osd_invariant(obj));
535
536         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
537         obj->oo_dt.do_body_ops = &osd_body_ops_new;
538         if (result == 0) {
539                 if (obj->oo_inode != NULL) {
540                         osd_object_init0(obj);
541                 } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
542                         obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
543                         /* LFSCK iterator object is special without inode */
544                         l->lo_header->loh_attr |= LOHA_EXISTS;
545                 }
546         }
547         LINVRNT(osd_invariant(obj));
548         return result;
549 }
550
551 /*
552  * Concurrency: no concurrent access is possible that late in object
553  * life-cycle.
554  */
555 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
556 {
557         struct osd_object *obj = osd_obj(l);
558
559         LINVRNT(osd_invariant(obj));
560
561         dt_object_fini(&obj->oo_dt);
562         if (obj->oo_hl_head != NULL)
563                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
564         OBD_FREE_PTR(obj);
565 }
566
567 /*
568  * Concurrency: no concurrent access is possible that late in object
569  * life-cycle.
570  */
571 static void osd_index_fini(struct osd_object *o)
572 {
573         struct iam_container *bag;
574
575         if (o->oo_dir != NULL) {
576                 bag = &o->oo_dir->od_container;
577                 if (o->oo_inode != NULL) {
578                         if (bag->ic_object == o->oo_inode)
579                                 iam_container_fini(bag);
580                 }
581                 OBD_FREE_PTR(o->oo_dir);
582                 o->oo_dir = NULL;
583         }
584 }
585
586 /*
587  * Concurrency: no concurrent access is possible that late in object
588  * life-cycle (for all existing callers, that is. New callers have to provide
589  * their own locking.)
590  */
591 static int osd_inode_unlinked(const struct inode *inode)
592 {
593         return inode->i_nlink == 0;
594 }
595
596 enum {
597         OSD_TXN_OI_DELETE_CREDITS    = 20,
598         OSD_TXN_INODE_DELETE_CREDITS = 20
599 };
600
601 /*
602  * Journal
603  */
604
605 #if OSD_THANDLE_STATS
606 /**
607  * Set time when the handle is allocated
608  */
609 static void osd_th_alloced(struct osd_thandle *oth)
610 {
611         oth->oth_alloced = cfs_time_current();
612 }
613
614 /**
615  * Set time when the handle started
616  */
617 static void osd_th_started(struct osd_thandle *oth)
618 {
619         oth->oth_started = cfs_time_current();
620 }
621
622 /**
623  * Helper function to convert time interval to microseconds packed in
624  * long int (default time units for the counter in "stats" initialized
625  * by lu_time_init() )
626  */
627 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
628 {
629         struct timeval val;
630
631         cfs_duration_usec(cfs_time_sub(end, start), &val);
632         return val.tv_sec * 1000000 + val.tv_usec;
633 }
634
635 /**
636  * Check whether the we deal with this handle for too long.
637  */
638 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
639                                 cfs_time_t alloced, cfs_time_t started,
640                                 cfs_time_t closed)
641 {
642         cfs_time_t now = cfs_time_current();
643
644         LASSERT(dev != NULL);
645
646         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
647                             interval_to_usec(alloced, started));
648         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
649                             interval_to_usec(started, closed));
650         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
651                             interval_to_usec(closed, now));
652
653         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
654                 CWARN("transaction handle %p was open for too long: "
655                       "now "CFS_TIME_T" ,"
656                       "alloced "CFS_TIME_T" ,"
657                       "started "CFS_TIME_T" ,"
658                       "closed "CFS_TIME_T"\n",
659                       oth, now, alloced, started, closed);
660                 libcfs_debug_dumpstack(NULL);
661         }
662 }
663
664 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
665 {                                                                       \
666         cfs_time_t __closed = cfs_time_current();                       \
667         cfs_time_t __alloced = oth->oth_alloced;                        \
668         cfs_time_t __started = oth->oth_started;                        \
669                                                                         \
670         expr;                                                           \
671         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
672 }
673
674 #else /* OSD_THANDLE_STATS */
675
676 #define osd_th_alloced(h)                  do {} while(0)
677 #define osd_th_started(h)                  do {} while(0)
678 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
679
680 #endif /* OSD_THANDLE_STATS */
681
682 /*
683  * Concurrency: doesn't access mutable data.
684  */
685 static int osd_param_is_sane(const struct osd_device *dev,
686                              const struct thandle *th)
687 {
688         struct osd_thandle *oh;
689         oh = container_of0(th, struct osd_thandle, ot_super);
690         return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
691 }
692
693 /*
694  * Concurrency: shouldn't matter.
695  */
696 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
697 static void osd_trans_commit_cb(struct super_block *sb,
698                                 struct journal_callback *jcb, int error)
699 #else
700 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
701 #endif
702 {
703         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
704         struct thandle     *th  = &oh->ot_super;
705         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
706         struct dt_txn_commit_cb *dcb, *tmp;
707
708         LASSERT(oh->ot_handle == NULL);
709
710         if (error)
711                 CERROR("transaction @0x%p commit error: %d\n", th, error);
712
713         dt_txn_hook_commit(th);
714
715         /* call per-transaction callbacks if any */
716         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
717                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
718                          "commit callback entry: magic=%x name='%s'\n",
719                          dcb->dcb_magic, dcb->dcb_name);
720                 cfs_list_del_init(&dcb->dcb_linkage);
721                 dcb->dcb_func(NULL, th, dcb, error);
722         }
723
724         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
725         lu_device_put(lud);
726         th->th_dev = NULL;
727
728         lu_context_exit(&th->th_ctx);
729         lu_context_fini(&th->th_ctx);
730         OBD_FREE_PTR(oh);
731 }
732
733 static struct thandle *osd_trans_create(const struct lu_env *env,
734                                         struct dt_device *d)
735 {
736         struct osd_thread_info *oti = osd_oti_get(env);
737         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
738         struct osd_thandle     *oh;
739         struct thandle         *th;
740         ENTRY;
741
742         /* on pending IO in this thread should left from prev. request */
743         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
744
745         th = ERR_PTR(-ENOMEM);
746         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
747         if (oh != NULL) {
748                 th = &oh->ot_super;
749                 th->th_dev = d;
750                 th->th_result = 0;
751                 th->th_tags = LCT_TX_HANDLE;
752                 oh->ot_credits = 0;
753                 oti->oti_dev = osd_dt_dev(d);
754                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
755                 osd_th_alloced(oh);
756         }
757         RETURN(th);
758 }
759
760 /*
761  * Concurrency: shouldn't matter.
762  */
763 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
764                     struct thandle *th)
765 {
766         struct osd_thread_info *oti = osd_oti_get(env);
767         struct osd_device  *dev = osd_dt_dev(d);
768         handle_t           *jh;
769         struct osd_thandle *oh;
770         int rc;
771
772         ENTRY;
773
774         LASSERT(current->journal_info == NULL);
775
776         oh = container_of0(th, struct osd_thandle, ot_super);
777         LASSERT(oh != NULL);
778         LASSERT(oh->ot_handle == NULL);
779
780         rc = dt_txn_hook_start(env, d, th);
781         if (rc != 0)
782                 GOTO(out, rc);
783
784         if (!osd_param_is_sane(dev, th)) {
785                 CWARN("%.16s: too many transaction credits (%d > %d)\n",
786                       LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
787                       oh->ot_credits,
788                       osd_journal(dev)->j_max_transaction_buffers);
789                 /* XXX Limit the credits to 'max_transaction_buffers', and
790                  *     let the underlying filesystem to catch the error if
791                  *     we really need so many credits.
792                  *
793                  *     This should be removed when we can calculate the
794                  *     credits precisely. */
795                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
796 #ifdef OSD_TRACK_DECLARES
797                 CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
798                        oh->ot_declare_attr_set, oh->ot_declare_punch,
799                        oh->ot_declare_xattr_set);
800                 CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
801                        oh->ot_declare_create, oh->ot_declare_ref_add,
802                        oh->ot_declare_ref_del, oh->ot_declare_write);
803                 CERROR("  insert: %d, delete: %d, destroy: %d\n",
804                        oh->ot_declare_insert, oh->ot_declare_delete,
805                        oh->ot_declare_destroy);
806 #endif
807         }
808
809         /*
810          * XXX temporary stuff. Some abstraction layer should
811          * be used.
812          */
813         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
814         osd_th_started(oh);
815         if (!IS_ERR(jh)) {
816                 oh->ot_handle = jh;
817                 LASSERT(oti->oti_txns == 0);
818                 lu_context_init(&th->th_ctx, th->th_tags);
819                 lu_context_enter(&th->th_ctx);
820
821                 lu_device_get(&d->dd_lu_dev);
822                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
823                                              "osd-tx", th);
824
825                 /*
826                  * XXX: current rule is that we first start tx,
827                  *      then lock object(s), but we can't use
828                  *      this rule for data (due to locking specifics
829                  *      in ldiskfs). also in long-term we'd like to
830                  *      use usually-used (locks;tx) ordering. so,
831                  *      UGLY thing is that we'll use one ordering for
832                  *      data (ofd) and reverse ordering for metadata
833                  *      (mdd). then at some point we'll fix the latter
834                  */
835                 if (dev->od_is_md) {
836                         LASSERT(oti->oti_r_locks == 0);
837                         LASSERT(oti->oti_w_locks == 0);
838                 }
839
840                 oti->oti_txns++;
841                 rc = 0;
842         } else {
843                 rc = PTR_ERR(jh);
844         }
845 out:
846         RETURN(rc);
847 }
848
849 /*
850  * Concurrency: shouldn't matter.
851  */
852 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
853 {
854         int                     rc = 0;
855         struct osd_thandle     *oh;
856         struct osd_thread_info *oti = osd_oti_get(env);
857         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
858
859         ENTRY;
860
861         oh = container_of0(th, struct osd_thandle, ot_super);
862
863         if (oh->ot_handle != NULL) {
864                 handle_t *hdl = oh->ot_handle;
865
866                 hdl->h_sync = th->th_sync;
867
868                 /*
869                  * add commit callback
870                  * notice we don't do this in osd_trans_start()
871                  * as underlying transaction can change during truncate
872                  */
873                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
874                                          &oh->ot_jcb);
875
876                 LASSERT(oti->oti_txns == 1);
877                 oti->oti_txns--;
878                 /*
879                  * XXX: current rule is that we first start tx,
880                  *      then lock object(s), but we can't use
881                  *      this rule for data (due to locking specifics
882                  *      in ldiskfs). also in long-term we'd like to
883                  *      use usually-used (locks;tx) ordering. so,
884                  *      UGLY thing is that we'll use one ordering for
885                  *      data (ofd) and reverse ordering for metadata
886                  *      (mdd). then at some point we'll fix the latter
887                  */
888                 if (osd_dt_dev(th->th_dev)->od_is_md) {
889                         LASSERT(oti->oti_r_locks == 0);
890                         LASSERT(oti->oti_w_locks == 0);
891                 }
892                 rc = dt_txn_hook_stop(env, th);
893                 if (rc != 0)
894                         CERROR("Failure in transaction hook: %d\n", rc);
895                 oh->ot_handle = NULL;
896                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
897                                   rc = ldiskfs_journal_stop(hdl));
898                 if (rc != 0)
899                         CERROR("Failure to stop transaction: %d\n", rc);
900         } else {
901                 OBD_FREE_PTR(oh);
902         }
903
904         /* as we want IO to journal and data IO be concurrent, we don't block
905          * awaiting data IO completion in osd_do_bio(), instead we wait here
906          * once transaction is submitted to the journal. all reqular requests
907          * don't do direct IO (except read/write), thus this wait_event becomes
908          * no-op for them.
909          *
910          * IMPORTANT: we have to wait till any IO submited by the thread is
911          * completed otherwise iobuf may be corrupted by different request
912          */
913         cfs_wait_event(iobuf->dr_wait,
914                        cfs_atomic_read(&iobuf->dr_numreqs) == 0);
915         if (!rc)
916                 rc = iobuf->dr_error;
917
918         RETURN(rc);
919 }
920
921 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
922 {
923         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
924                                                ot_super);
925
926         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
927         LASSERT(&dcb->dcb_func != NULL);
928         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
929
930         return 0;
931 }
932
933 /*
934  * Called just before object is freed. Releases all resources except for
935  * object itself (that is released by osd_object_free()).
936  *
937  * Concurrency: no concurrent access is possible that late in object
938  * life-cycle.
939  */
940 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
941 {
942         struct osd_object *obj   = osd_obj(l);
943         struct inode      *inode = obj->oo_inode;
944
945         LINVRNT(osd_invariant(obj));
946
947         /*
948          * If object is unlinked remove fid->ino mapping from object index.
949          */
950
951         osd_index_fini(obj);
952         if (inode != NULL) {
953                 iput(inode);
954                 obj->oo_inode = NULL;
955         }
956 }
957
958 /*
959  * Concurrency: ->loo_object_release() is called under site spin-lock.
960  */
961 static void osd_object_release(const struct lu_env *env,
962                                struct lu_object *l)
963 {
964 }
965
966 /*
967  * Concurrency: shouldn't matter.
968  */
969 static int osd_object_print(const struct lu_env *env, void *cookie,
970                             lu_printer_t p, const struct lu_object *l)
971 {
972         struct osd_object *o = osd_obj(l);
973         struct iam_descr  *d;
974
975         if (o->oo_dir != NULL)
976                 d = o->oo_dir->od_container.ic_descr;
977         else
978                 d = NULL;
979         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
980                     o, o->oo_inode,
981                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
982                     o->oo_inode ? o->oo_inode->i_generation : 0,
983                     d ? d->id_ops->id_name : "plain");
984 }
985
986 /*
987  * Concurrency: shouldn't matter.
988  */
989 int osd_statfs(const struct lu_env *env, struct dt_device *d,
990                struct obd_statfs *sfs)
991 {
992         struct osd_device  *osd = osd_dt_dev(d);
993         struct super_block *sb = osd_sb(osd);
994         struct kstatfs     *ksfs;
995         int result = 0;
996
997         if (unlikely(osd->od_mount == NULL))
998                 return -EINPROGRESS;
999
1000         /* osd_lproc.c call this without env, allocate ksfs for that case */
1001         if (unlikely(env == NULL)) {
1002                 OBD_ALLOC_PTR(ksfs);
1003                 if (ksfs == NULL)
1004                         return -ENOMEM;
1005         } else {
1006                 ksfs = &osd_oti_get(env)->oti_ksfs;
1007         }
1008
1009         cfs_spin_lock(&osd->od_osfs_lock);
1010         /* cache 1 second */
1011         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1012                 result = sb->s_op->statfs(sb->s_root, ksfs);
1013                 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1014                         osd->od_osfs_age = cfs_time_current_64();
1015                         statfs_pack(&osd->od_statfs, ksfs);
1016                 }
1017         }
1018
1019         if (likely(result == 0))
1020                 *sfs = osd->od_statfs;
1021         cfs_spin_unlock(&osd->od_osfs_lock);
1022
1023         if (unlikely(env == NULL))
1024                 OBD_FREE_PTR(ksfs);
1025
1026         return result;
1027 }
1028
1029 /*
1030  * Concurrency: doesn't access mutable data.
1031  */
1032 static void osd_conf_get(const struct lu_env *env,
1033                          const struct dt_device *dev,
1034                          struct dt_device_param *param)
1035 {
1036         struct super_block *sb = osd_sb(osd_dt_dev(dev));
1037
1038         /*
1039          * XXX should be taken from not-yet-existing fs abstraction layer.
1040          */
1041         param->ddp_max_name_len = LDISKFS_NAME_LEN;
1042         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1043         param->ddp_block_shift  = sb->s_blocksize_bits;
1044         param->ddp_mntopts      = 0;
1045         if (test_opt(sb, XATTR_USER))
1046                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1047         if (test_opt(sb, POSIX_ACL))
1048                 param->ddp_mntopts |= MNTOPT_ACL;
1049
1050 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1051         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1052                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1053         else
1054 #endif
1055                 param->ddp_max_ea_size = sb->s_blocksize;
1056
1057 }
1058
1059 /**
1060  * Helper function to get and fill the buffer with input values.
1061  */
1062 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1063 {
1064         struct lu_buf *buf;
1065
1066         buf = &osd_oti_get(env)->oti_buf;
1067         buf->lb_buf = area;
1068         buf->lb_len = len;
1069         return buf;
1070 }
1071
1072 /*
1073  * Concurrency: shouldn't matter.
1074  */
1075 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1076 {
1077         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1078         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1079 }
1080
1081 /**
1082  * Start commit for OSD device.
1083  *
1084  * An implementation of dt_commit_async method for OSD device.
1085  * Asychronously starts underlayng fs sync and thereby a transaction
1086  * commit.
1087  *
1088  * \param env environment
1089  * \param d dt device
1090  *
1091  * \see dt_device_operations
1092  */
1093 static int osd_commit_async(const struct lu_env *env,
1094                             struct dt_device *d)
1095 {
1096         struct super_block *s = osd_sb(osd_dt_dev(d));
1097         ENTRY;
1098
1099         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1100         RETURN(s->s_op->sync_fs(s, 0));
1101 }
1102
1103 /*
1104  * Concurrency: shouldn't matter.
1105  */
1106
1107 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1108 {
1109         struct super_block *sb = osd_sb(osd_dt_dev(d));
1110         int rc;
1111         ENTRY;
1112
1113         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1114
1115         rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1116         RETURN(rc);
1117 }
1118
1119 /*
1120  * Concurrency: serialization provided by callers.
1121  */
1122 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1123                               int mode, unsigned long timeout, __u32 alg,
1124                               struct lustre_capa_key *keys)
1125 {
1126         struct osd_device *dev = osd_dt_dev(d);
1127         ENTRY;
1128
1129         dev->od_fl_capa = mode;
1130         dev->od_capa_timeout = timeout;
1131         dev->od_capa_alg = alg;
1132         dev->od_capa_keys = keys;
1133         RETURN(0);
1134 }
1135
1136 /**
1137  * Concurrency: serialization provided by callers.
1138  */
1139 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1140                                struct dt_quota_ctxt *ctxt, void *data)
1141 {
1142         struct obd_device *obd = (void *)ctxt;
1143         struct vfsmount *mnt = (struct vfsmount *)data;
1144         ENTRY;
1145
1146         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1147         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1148         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1149         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1150         obd->obd_lvfs_ctxt.fs = get_ds();
1151
1152         EXIT;
1153 }
1154
1155 /**
1156  * Note: we do not count into QUOTA here.
1157  * If we mount with --data_journal we may need more.
1158  */
1159 const int osd_dto_credits_noquota[DTO_NR] = {
1160         /**
1161          * Insert/Delete.
1162          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1163          * SINGLEDATA_TRANS_BLOCKS(8)
1164          * XXX Note: maybe iam need more, since iam have more level than
1165          *           EXT3 htree.
1166          */
1167         [DTO_INDEX_INSERT]  = 16,
1168         [DTO_INDEX_DELETE]  = 16,
1169         /**
1170          * Used for OI scrub
1171          */
1172         [DTO_INDEX_UPDATE]  = 16,
1173         /**
1174          * Create a object. The same as create object in EXT3.
1175          * DATA_TRANS_BLOCKS(14) +
1176          * INDEX_EXTRA_BLOCKS(8) +
1177          * 3(inode bits, groups, GDT)
1178          */
1179         [DTO_OBJECT_CREATE] = 25,
1180         /**
1181          * XXX: real credits to be fixed
1182          */
1183         [DTO_OBJECT_DELETE] = 25,
1184         /**
1185          * Attr set credits (inode)
1186          */
1187         [DTO_ATTR_SET_BASE] = 1,
1188         /**
1189          * Xattr set. The same as xattr of EXT3.
1190          * DATA_TRANS_BLOCKS(14)
1191          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1192          * are also counted in. Do not know why?
1193          */
1194         [DTO_XATTR_SET]     = 14,
1195         [DTO_LOG_REC]       = 14,
1196         /**
1197          * credits for inode change during write.
1198          */
1199         [DTO_WRITE_BASE]    = 3,
1200         /**
1201          * credits for single block write.
1202          */
1203         [DTO_WRITE_BLOCK]   = 14,
1204         /**
1205          * Attr set credits for chown.
1206          * This is extra credits for setattr, and it is null without quota
1207          */
1208         [DTO_ATTR_SET_CHOWN]= 0
1209 };
1210
1211 static const struct dt_device_operations osd_dt_ops = {
1212         .dt_root_get       = osd_root_get,
1213         .dt_statfs         = osd_statfs,
1214         .dt_trans_create   = osd_trans_create,
1215         .dt_trans_start    = osd_trans_start,
1216         .dt_trans_stop     = osd_trans_stop,
1217         .dt_trans_cb_add   = osd_trans_cb_add,
1218         .dt_conf_get       = osd_conf_get,
1219         .dt_sync           = osd_sync,
1220         .dt_ro             = osd_ro,
1221         .dt_commit_async   = osd_commit_async,
1222         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1223         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1224 };
1225
1226 static void osd_object_read_lock(const struct lu_env *env,
1227                                  struct dt_object *dt, unsigned role)
1228 {
1229         struct osd_object *obj = osd_dt_obj(dt);
1230         struct osd_thread_info *oti = osd_oti_get(env);
1231
1232         LINVRNT(osd_invariant(obj));
1233
1234         LASSERT(obj->oo_owner != env);
1235         cfs_down_read_nested(&obj->oo_sem, role);
1236
1237         LASSERT(obj->oo_owner == NULL);
1238         oti->oti_r_locks++;
1239 }
1240
1241 static void osd_object_write_lock(const struct lu_env *env,
1242                                   struct dt_object *dt, unsigned role)
1243 {
1244         struct osd_object *obj = osd_dt_obj(dt);
1245         struct osd_thread_info *oti = osd_oti_get(env);
1246
1247         LINVRNT(osd_invariant(obj));
1248
1249         LASSERT(obj->oo_owner != env);
1250         cfs_down_write_nested(&obj->oo_sem, role);
1251
1252         LASSERT(obj->oo_owner == NULL);
1253         obj->oo_owner = env;
1254         oti->oti_w_locks++;
1255 }
1256
1257 static void osd_object_read_unlock(const struct lu_env *env,
1258                                    struct dt_object *dt)
1259 {
1260         struct osd_object *obj = osd_dt_obj(dt);
1261         struct osd_thread_info *oti = osd_oti_get(env);
1262
1263         LINVRNT(osd_invariant(obj));
1264
1265         LASSERT(oti->oti_r_locks > 0);
1266         oti->oti_r_locks--;
1267         cfs_up_read(&obj->oo_sem);
1268 }
1269
1270 static void osd_object_write_unlock(const struct lu_env *env,
1271                                     struct dt_object *dt)
1272 {
1273         struct osd_object *obj = osd_dt_obj(dt);
1274         struct osd_thread_info *oti = osd_oti_get(env);
1275
1276         LINVRNT(osd_invariant(obj));
1277
1278         LASSERT(obj->oo_owner == env);
1279         LASSERT(oti->oti_w_locks > 0);
1280         oti->oti_w_locks--;
1281         obj->oo_owner = NULL;
1282         cfs_up_write(&obj->oo_sem);
1283 }
1284
1285 static int osd_object_write_locked(const struct lu_env *env,
1286                                    struct dt_object *dt)
1287 {
1288         struct osd_object *obj = osd_dt_obj(dt);
1289
1290         LINVRNT(osd_invariant(obj));
1291
1292         return obj->oo_owner == env;
1293 }
1294
1295 static int capa_is_sane(const struct lu_env *env,
1296                         struct osd_device *dev,
1297                         struct lustre_capa *capa,
1298                         struct lustre_capa_key *keys)
1299 {
1300         struct osd_thread_info *oti = osd_oti_get(env);
1301         struct lustre_capa *tcapa = &oti->oti_capa;
1302         struct obd_capa *oc;
1303         int i, rc = 0;
1304         ENTRY;
1305
1306         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1307         if (oc) {
1308                 if (capa_is_expired(oc)) {
1309                         DEBUG_CAPA(D_ERROR, capa, "expired");
1310                         rc = -ESTALE;
1311                 }
1312                 capa_put(oc);
1313                 RETURN(rc);
1314         }
1315
1316         if (capa_is_expired_sec(capa)) {
1317                 DEBUG_CAPA(D_ERROR, capa, "expired");
1318                 RETURN(-ESTALE);
1319         }
1320
1321         cfs_spin_lock(&capa_lock);
1322         for (i = 0; i < 2; i++) {
1323                 if (keys[i].lk_keyid == capa->lc_keyid) {
1324                         oti->oti_capa_key = keys[i];
1325                         break;
1326                 }
1327         }
1328         cfs_spin_unlock(&capa_lock);
1329
1330         if (i == 2) {
1331                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1332                 RETURN(-ESTALE);
1333         }
1334
1335         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1336         if (rc)
1337                 RETURN(rc);
1338
1339         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1340                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1341                 RETURN(-EACCES);
1342         }
1343
1344         oc = capa_add(dev->od_capa_hash, capa);
1345         capa_put(oc);
1346
1347         RETURN(0);
1348 }
1349
1350 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1351                     struct lustre_capa *capa, __u64 opc)
1352 {
1353         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1354         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1355         struct md_capainfo *ci;
1356         int rc;
1357
1358         if (!dev->od_fl_capa)
1359                 return 0;
1360
1361         if (capa == BYPASS_CAPA)
1362                 return 0;
1363
1364         ci = md_capainfo(env);
1365         if (unlikely(!ci))
1366                 return 0;
1367
1368         if (ci->mc_auth == LC_ID_NONE)
1369                 return 0;
1370
1371         if (!capa) {
1372                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1373                 return -EACCES;
1374         }
1375
1376         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1377                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1378                            PFID(fid));
1379                 return -EACCES;
1380         }
1381
1382         if (!capa_opc_supported(capa, opc)) {
1383                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1384                 return -EACCES;
1385         }
1386
1387         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1388                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1389                 return -EACCES;
1390         }
1391
1392         return 0;
1393 }
1394
1395 static struct timespec *osd_inode_time(const struct lu_env *env,
1396                                        struct inode *inode, __u64 seconds)
1397 {
1398         struct osd_thread_info  *oti = osd_oti_get(env);
1399         struct timespec         *t   = &oti->oti_time;
1400
1401         t->tv_sec = seconds;
1402         t->tv_nsec = 0;
1403         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1404         return t;
1405 }
1406
1407
1408 static void osd_inode_getattr(const struct lu_env *env,
1409                               struct inode *inode, struct lu_attr *attr)
1410 {
1411         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1412                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1413                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1414
1415         attr->la_atime      = LTIME_S(inode->i_atime);
1416         attr->la_mtime      = LTIME_S(inode->i_mtime);
1417         attr->la_ctime      = LTIME_S(inode->i_ctime);
1418         attr->la_mode       = inode->i_mode;
1419         attr->la_size       = i_size_read(inode);
1420         attr->la_blocks     = inode->i_blocks;
1421         attr->la_uid        = inode->i_uid;
1422         attr->la_gid        = inode->i_gid;
1423         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1424         attr->la_nlink      = inode->i_nlink;
1425         attr->la_rdev       = inode->i_rdev;
1426         attr->la_blksize    = 1 << inode->i_blkbits;
1427         attr->la_blkbits    = inode->i_blkbits;
1428 }
1429
1430 static int osd_attr_get(const struct lu_env *env,
1431                         struct dt_object *dt,
1432                         struct lu_attr *attr,
1433                         struct lustre_capa *capa)
1434 {
1435         struct osd_object *obj = osd_dt_obj(dt);
1436
1437         LASSERT(dt_object_exists(dt));
1438         LINVRNT(osd_invariant(obj));
1439
1440         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1441                 return -EACCES;
1442
1443         cfs_spin_lock(&obj->oo_guard);
1444         osd_inode_getattr(env, obj->oo_inode, attr);
1445         cfs_spin_unlock(&obj->oo_guard);
1446         return 0;
1447 }
1448
1449 static int osd_declare_attr_set(const struct lu_env *env,
1450                                 struct dt_object *dt,
1451                                 const struct lu_attr *attr,
1452                                 struct thandle *handle)
1453 {
1454         struct osd_thandle *oh;
1455         struct osd_object *obj;
1456
1457         LASSERT(dt != NULL);
1458         LASSERT(handle != NULL);
1459
1460         obj = osd_dt_obj(dt);
1461         LASSERT(osd_invariant(obj));
1462
1463         oh = container_of0(handle, struct osd_thandle, ot_super);
1464         LASSERT(oh->ot_handle == NULL);
1465
1466         OSD_DECLARE_OP(oh, attr_set);
1467         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1468
1469         if (attr && attr->la_valid & LA_UID) {
1470                 if (obj->oo_inode)
1471                         osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1472                                         obj->oo_inode);
1473                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1474         }
1475         if (attr && attr->la_valid & LA_GID) {
1476                 if (obj->oo_inode)
1477                         osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1478                                         obj->oo_inode);
1479                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1480         }
1481
1482         return 0;
1483 }
1484
1485 static int osd_inode_setattr(const struct lu_env *env,
1486                              struct inode *inode, const struct lu_attr *attr)
1487 {
1488         __u64 bits;
1489
1490         bits = attr->la_valid;
1491
1492         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1493
1494         if (bits & LA_ATIME)
1495                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1496         if (bits & LA_CTIME)
1497                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1498         if (bits & LA_MTIME)
1499                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1500         if (bits & LA_SIZE) {
1501                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1502                 i_size_write(inode, attr->la_size);
1503         }
1504
1505 #if 0
1506         /* OSD should not change "i_blocks" which is used by quota.
1507          * "i_blocks" should be changed by ldiskfs only. */
1508         if (bits & LA_BLOCKS)
1509                 inode->i_blocks = attr->la_blocks;
1510 #endif
1511         if (bits & LA_MODE)
1512                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1513                         (attr->la_mode & ~S_IFMT);
1514         if (bits & LA_UID)
1515                 inode->i_uid    = attr->la_uid;
1516         if (bits & LA_GID)
1517                 inode->i_gid    = attr->la_gid;
1518         if (bits & LA_NLINK)
1519                 inode->i_nlink  = attr->la_nlink;
1520         if (bits & LA_RDEV)
1521                 inode->i_rdev   = attr->la_rdev;
1522
1523         if (bits & LA_FLAGS) {
1524                 /* always keep S_NOCMTIME */
1525                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1526                                  S_NOCMTIME;
1527         }
1528         return 0;
1529 }
1530
1531 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1532 {
1533         if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1534             (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1535                 struct iattr    iattr;
1536                 int             rc;
1537
1538                 iattr.ia_valid = 0;
1539                 if (attr->la_valid & LA_UID)
1540                         iattr.ia_valid |= ATTR_UID;
1541                 if (attr->la_valid & LA_GID)
1542                         iattr.ia_valid |= ATTR_GID;
1543                 iattr.ia_uid = attr->la_uid;
1544                 iattr.ia_gid = attr->la_gid;
1545
1546                 rc = ll_vfs_dq_transfer(inode, &iattr);
1547                 if (rc) {
1548                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
1549                                "enforcement enabled on the ldiskfs filesystem?",
1550                                inode->i_sb->s_id, rc);
1551                         return rc;
1552                 }
1553         }
1554         return 0;
1555 }
1556
1557 static int osd_attr_set(const struct lu_env *env,
1558                         struct dt_object *dt,
1559                         const struct lu_attr *attr,
1560                         struct thandle *handle,
1561                         struct lustre_capa *capa)
1562 {
1563         struct osd_object *obj = osd_dt_obj(dt);
1564         struct inode      *inode;
1565         int rc;
1566
1567         LASSERT(handle != NULL);
1568         LASSERT(dt_object_exists(dt));
1569         LASSERT(osd_invariant(obj));
1570
1571         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1572                 return -EACCES;
1573
1574         OSD_EXEC_OP(handle, attr_set);
1575
1576         inode = obj->oo_inode;
1577         if (!osd_dt_dev(handle->th_dev)->od_is_md) {
1578                 /* OFD support */
1579                 rc = osd_quota_transfer(inode, attr);
1580                 if (rc)
1581                         return rc;
1582         } else {
1583 #ifdef HAVE_QUOTA_SUPPORT
1584                 if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1585                     (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1586                         struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1587                         struct           iattr iattr;
1588                         int              rc;
1589
1590                         iattr.ia_valid = 0;
1591                         if (attr->la_valid & LA_UID)
1592                                 iattr.ia_valid |= ATTR_UID;
1593                         if (attr->la_valid & LA_GID)
1594                                 iattr.ia_valid |= ATTR_GID;
1595                         iattr.ia_uid = attr->la_uid;
1596                         iattr.ia_gid = attr->la_gid;
1597                         osd_push_ctxt(env, save, 1);
1598                         rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1599                         osd_pop_ctxt(save, 1);
1600                         if (rc != 0)
1601                                 return rc;
1602                 }
1603 #endif
1604         }
1605         cfs_spin_lock(&obj->oo_guard);
1606         rc = osd_inode_setattr(env, inode, attr);
1607         cfs_spin_unlock(&obj->oo_guard);
1608
1609         if (!rc)
1610                 inode->i_sb->s_op->dirty_inode(inode);
1611         return rc;
1612 }
1613
1614 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1615                                     struct osd_object *obj,
1616                                     const char *name, const int namelen)
1617 {
1618         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1619 }
1620
1621 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1622                       cfs_umode_t mode,
1623                       struct dt_allocation_hint *hint,
1624                       struct thandle *th)
1625 {
1626         int result;
1627         struct osd_device  *osd = osd_obj2dev(obj);
1628         struct osd_thandle *oth;
1629         struct dt_object   *parent = NULL;
1630         struct inode       *inode;
1631 #ifdef HAVE_QUOTA_SUPPORT
1632         struct osd_ctxt    *save = &info->oti_ctxt;
1633 #endif
1634
1635         LINVRNT(osd_invariant(obj));
1636         LASSERT(obj->oo_inode == NULL);
1637         LASSERT(obj->oo_hl_head == NULL);
1638
1639         if (S_ISDIR(mode) && ldiskfs_pdo) {
1640                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1641                 if (obj->oo_hl_head == NULL)
1642                         return -ENOMEM;
1643         }
1644
1645         oth = container_of(th, struct osd_thandle, ot_super);
1646         LASSERT(oth->ot_handle->h_transaction != NULL);
1647
1648         if (hint && hint->dah_parent)
1649                 parent = hint->dah_parent;
1650
1651 #ifdef HAVE_QUOTA_SUPPORT
1652         osd_push_ctxt(info->oti_env, save, osd_dt_dev(th->th_dev)->od_is_md);
1653 #endif
1654         inode = ldiskfs_create_inode(oth->ot_handle,
1655                                      parent ? osd_dt_obj(parent)->oo_inode :
1656                                               osd_sb(osd)->s_root->d_inode,
1657                                      mode);
1658 #ifdef HAVE_QUOTA_SUPPORT
1659         osd_pop_ctxt(save, osd_dt_dev(th->th_dev)->od_is_md);
1660 #endif
1661         if (!IS_ERR(inode)) {
1662                 /* Do not update file c/mtime in ldiskfs.
1663                  * NB: don't need any lock because no contention at this
1664                  * early stage */
1665                 inode->i_flags |= S_NOCMTIME;
1666                 inode->i_state |= I_LUSTRE_NOSCRUB;
1667                 obj->oo_inode = inode;
1668                 result = 0;
1669         } else {
1670                 if (obj->oo_hl_head != NULL) {
1671                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1672                         obj->oo_hl_head = NULL;
1673                 }
1674                 result = PTR_ERR(inode);
1675         }
1676         LINVRNT(osd_invariant(obj));
1677         return result;
1678 }
1679
1680 enum {
1681         OSD_NAME_LEN = 255
1682 };
1683
1684 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1685                      struct lu_attr *attr,
1686                      struct dt_allocation_hint *hint,
1687                      struct dt_object_format *dof,
1688                      struct thandle *th)
1689 {
1690         int result;
1691         struct osd_thandle *oth;
1692         struct osd_device *osd = osd_obj2dev(obj);
1693         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1694
1695         LASSERT(S_ISDIR(attr->la_mode));
1696
1697         oth = container_of(th, struct osd_thandle, ot_super);
1698         LASSERT(oth->ot_handle->h_transaction != NULL);
1699         result = osd_mkfile(info, obj, mode, hint, th);
1700         if (result == 0 && osd->od_iop_mode == 0) {
1701                 LASSERT(obj->oo_inode != NULL);
1702                 /*
1703                  * XXX uh-oh... call low-level iam function directly.
1704                  */
1705
1706                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1707                                          sizeof (struct osd_fid_pack),
1708                                          oth->ot_handle);
1709         }
1710         return result;
1711 }
1712
1713 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1714                         struct lu_attr *attr,
1715                         struct dt_allocation_hint *hint,
1716                         struct dt_object_format *dof,
1717                         struct thandle *th)
1718 {
1719         int result;
1720         struct osd_thandle *oth;
1721         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1722
1723         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1724
1725         LASSERT(S_ISREG(attr->la_mode));
1726
1727         oth = container_of(th, struct osd_thandle, ot_super);
1728         LASSERT(oth->ot_handle->h_transaction != NULL);
1729
1730         result = osd_mkfile(info, obj, mode, hint, th);
1731         if (result == 0) {
1732                 LASSERT(obj->oo_inode != NULL);
1733                 if (feat->dif_flags & DT_IND_VARKEY)
1734                         result = iam_lvar_create(obj->oo_inode,
1735                                                  feat->dif_keysize_max,
1736                                                  feat->dif_ptrsize,
1737                                                  feat->dif_recsize_max,
1738                                                  oth->ot_handle);
1739                 else
1740                         result = iam_lfix_create(obj->oo_inode,
1741                                                  feat->dif_keysize_max,
1742                                                  feat->dif_ptrsize,
1743                                                  feat->dif_recsize_max,
1744                                                  oth->ot_handle);
1745
1746         }
1747         return result;
1748 }
1749
1750 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1751                      struct lu_attr *attr,
1752                      struct dt_allocation_hint *hint,
1753                      struct dt_object_format *dof,
1754                      struct thandle *th)
1755 {
1756         LASSERT(S_ISREG(attr->la_mode));
1757         return osd_mkfile(info, obj, (attr->la_mode &
1758                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1759 }
1760
1761 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1762                      struct lu_attr *attr,
1763                      struct dt_allocation_hint *hint,
1764                      struct dt_object_format *dof,
1765                      struct thandle *th)
1766 {
1767         LASSERT(S_ISLNK(attr->la_mode));
1768         return osd_mkfile(info, obj, (attr->la_mode &
1769                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1770 }
1771
1772 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1773                      struct lu_attr *attr,
1774                      struct dt_allocation_hint *hint,
1775                      struct dt_object_format *dof,
1776                      struct thandle *th)
1777 {
1778         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1779         int result;
1780
1781         LINVRNT(osd_invariant(obj));
1782         LASSERT(obj->oo_inode == NULL);
1783         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1784                 S_ISFIFO(mode) || S_ISSOCK(mode));
1785
1786         result = osd_mkfile(info, obj, mode, hint, th);
1787         if (result == 0) {
1788                 LASSERT(obj->oo_inode != NULL);
1789                 /*
1790                  * This inode should be marked dirty for i_rdev.  Currently
1791                  * that is done in the osd_attr_init().
1792                  */
1793                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1794         }
1795         LINVRNT(osd_invariant(obj));
1796         return result;
1797 }
1798
1799 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1800                               struct lu_attr *,
1801                               struct dt_allocation_hint *hint,
1802                               struct dt_object_format *dof,
1803                               struct thandle *);
1804
1805 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1806 {
1807         osd_obj_type_f result;
1808
1809         switch (type) {
1810         case DFT_DIR:
1811                 result = osd_mkdir;
1812                 break;
1813         case DFT_REGULAR:
1814                 result = osd_mkreg;
1815                 break;
1816         case DFT_SYM:
1817                 result = osd_mksym;
1818                 break;
1819         case DFT_NODE:
1820                 result = osd_mknod;
1821                 break;
1822         case DFT_INDEX:
1823                 result = osd_mk_index;
1824                 break;
1825
1826         default:
1827                 LBUG();
1828                 break;
1829         }
1830         return result;
1831 }
1832
1833
1834 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1835                         struct dt_object *parent, cfs_umode_t child_mode)
1836 {
1837         LASSERT(ah);
1838
1839         memset(ah, 0, sizeof(*ah));
1840         ah->dah_parent = parent;
1841         ah->dah_mode = child_mode;
1842 }
1843
1844 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1845                           struct lu_attr *attr, struct dt_object_format *dof)
1846 {
1847         struct inode   *inode = obj->oo_inode;
1848         __u64           valid = attr->la_valid;
1849         int             result;
1850
1851         attr->la_valid &= ~(LA_TYPE | LA_MODE);
1852
1853         if (dof->dof_type != DFT_NODE)
1854                 attr->la_valid &= ~LA_RDEV;
1855         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1856                 attr->la_valid &= ~LA_ATIME;
1857         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1858                 attr->la_valid &= ~LA_CTIME;
1859         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1860                 attr->la_valid &= ~LA_MTIME;
1861
1862         if (!osd_obj2dev(obj)->od_is_md) {
1863                 /* OFD support */
1864                 result = osd_quota_transfer(inode, attr);
1865                 if (result)
1866                         return;
1867         } else {
1868 #ifdef HAVE_QUOTA_SUPPORT
1869                 attr->la_valid &= ~(LA_UID | LA_GID);
1870 #endif
1871         }
1872
1873         if (attr->la_valid != 0) {
1874                 result = osd_inode_setattr(info->oti_env, inode, attr);
1875                 /*
1876                  * The osd_inode_setattr() should always succeed here.  The
1877                  * only error that could be returned is EDQUOT when we are
1878                  * trying to change the UID or GID of the inode. However, this
1879                  * should not happen since quota enforcement is no longer
1880                  * enabled on ldiskfs (lquota takes care of it).
1881                  */
1882                 LASSERTF(result == 0, "%d", result);
1883                 inode->i_sb->s_op->dirty_inode(inode);
1884         }
1885
1886         attr->la_valid = valid;
1887 }
1888
1889 /**
1890  * Helper function for osd_object_create()
1891  *
1892  * \retval 0, on success
1893  */
1894 static int __osd_object_create(struct osd_thread_info *info,
1895                                struct osd_object *obj, struct lu_attr *attr,
1896                                struct dt_allocation_hint *hint,
1897                                struct dt_object_format *dof,
1898                                struct thandle *th)
1899 {
1900         int     result;
1901         __u32   umask;
1902
1903         /* we drop umask so that permissions we pass are not affected */
1904         umask = current->fs->umask;
1905         current->fs->umask = 0;
1906
1907         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1908                                                   th);
1909         if (result == 0) {
1910                 osd_attr_init(info, obj, attr, dof);
1911                 osd_object_init0(obj);
1912                 /* bz 24037 */
1913                 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1914                         unlock_new_inode(obj->oo_inode);
1915         }
1916
1917         /* restore previous umask value */
1918         current->fs->umask = umask;
1919
1920         return result;
1921 }
1922
1923 /**
1924  * Helper function for osd_object_create()
1925  *
1926  * \retval 0, on success
1927  */
1928 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1929                            const struct lu_fid *fid, struct thandle *th)
1930 {
1931         struct osd_thread_info *info = osd_oti_get(env);
1932         struct osd_inode_id    *id   = &info->oti_id;
1933         struct osd_device      *osd  = osd_obj2dev(obj);
1934
1935         LASSERT(obj->oo_inode != NULL);
1936
1937         if (osd->od_is_md) {
1938                 struct md_ucred *uc = md_ucred(env);
1939                 LASSERT(uc != NULL);
1940         }
1941
1942         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1943         return osd_oi_insert(info, osd, fid, id, th);
1944 }
1945
1946 static int osd_declare_object_create(const struct lu_env *env,
1947                                      struct dt_object *dt,
1948                                      struct lu_attr *attr,
1949                                      struct dt_allocation_hint *hint,
1950                                      struct dt_object_format *dof,
1951                                      struct thandle *handle)
1952 {
1953         struct osd_thandle *oh;
1954
1955         LASSERT(handle != NULL);
1956
1957         oh = container_of0(handle, struct osd_thandle, ot_super);
1958         LASSERT(oh->ot_handle == NULL);
1959
1960         OSD_DECLARE_OP(oh, create);
1961         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1962         /* XXX: So far, only normal fid needs be inserted into the oi,
1963          *      things could be changed later. Revise following code then. */
1964         if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1965                 OSD_DECLARE_OP(oh, insert);
1966                 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1967         }
1968         /* If this is directory, then we expect . and .. to be inserted as
1969          * well. The one directory block always needs to be created for the
1970          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1971          * block), there is no danger of needing a tree for the first block.
1972          */
1973         if (attr && S_ISDIR(attr->la_mode)) {
1974                 OSD_DECLARE_OP(oh, insert);
1975                 OSD_DECLARE_OP(oh, insert);
1976                 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1977         }
1978
1979         if (attr) {
1980                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1981                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1982         }
1983         return 0;
1984 }
1985
1986 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1987                              struct lu_attr *attr,
1988                              struct dt_allocation_hint *hint,
1989                              struct dt_object_format *dof,
1990                              struct thandle *th)
1991 {
1992         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1993         struct osd_object      *obj    = osd_dt_obj(dt);
1994         struct osd_thread_info *info   = osd_oti_get(env);
1995         int result;
1996
1997         ENTRY;
1998
1999         LINVRNT(osd_invariant(obj));
2000         LASSERT(!dt_object_exists(dt));
2001         LASSERT(osd_write_locked(env, obj));
2002         LASSERT(th != NULL);
2003
2004         if (unlikely(fid_is_acct(fid)))
2005                 /* Quota files can't be created from the kernel any more,
2006                  * 'tune2fs -O quota' will take care of creating them */
2007                 RETURN(-EPERM);
2008
2009         OSD_EXEC_OP(th, create);
2010
2011         result = __osd_object_create(info, obj, attr, hint, dof, th);
2012         if (result == 0)
2013                 result = __osd_oi_insert(env, obj, fid, th);
2014
2015         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2016         LASSERT(osd_invariant(obj));
2017         RETURN(result);
2018 }
2019
2020 /**
2021  * Called to destroy on-disk representation of the object
2022  *
2023  * Concurrency: must be locked
2024  */
2025 static int osd_declare_object_destroy(const struct lu_env *env,
2026                                       struct dt_object *dt,
2027                                       struct thandle *th)
2028 {
2029         struct osd_object  *obj = osd_dt_obj(dt);
2030         struct inode       *inode = obj->oo_inode;
2031         struct osd_thandle *oh;
2032
2033         ENTRY;
2034
2035         oh = container_of0(th, struct osd_thandle, ot_super);
2036         LASSERT(oh->ot_handle == NULL);
2037         LASSERT(inode);
2038
2039         OSD_DECLARE_OP(oh, destroy);
2040         OSD_DECLARE_OP(oh, delete);
2041         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2042         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2043
2044         osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2045         osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2046
2047         RETURN(0);
2048 }
2049
2050 static int osd_object_destroy(const struct lu_env *env,
2051                               struct dt_object *dt,
2052                               struct thandle *th)
2053 {
2054         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2055         struct osd_object      *obj = osd_dt_obj(dt);
2056         struct inode           *inode = obj->oo_inode;
2057         struct osd_device      *osd = osd_obj2dev(obj);
2058         struct osd_thandle     *oh;
2059         int                     result;
2060         ENTRY;
2061
2062         oh = container_of0(th, struct osd_thandle, ot_super);
2063         LASSERT(oh->ot_handle);
2064         LASSERT(inode);
2065         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2066
2067         if (unlikely(fid_is_acct(fid)))
2068                 RETURN(-EPERM);
2069
2070         /* Parallel control for OI scrub. For most of cases, there is no
2071          * lock contention. So it will not affect unlink performance. */
2072         cfs_mutex_lock(&inode->i_mutex);
2073         if (S_ISDIR(inode->i_mode)) {
2074                 LASSERT(osd_inode_unlinked(inode) ||
2075                         inode->i_nlink == 1);
2076                 cfs_spin_lock(&obj->oo_guard);
2077                 inode->i_nlink = 0;
2078                 cfs_spin_unlock(&obj->oo_guard);
2079                 inode->i_sb->s_op->dirty_inode(inode);
2080         } else {
2081                 LASSERT(osd_inode_unlinked(inode));
2082         }
2083
2084         OSD_EXEC_OP(th, destroy);
2085
2086         result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2087         cfs_mutex_unlock(&inode->i_mutex);
2088
2089         /* XXX: add to ext3 orphan list */
2090         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2091
2092         /* not needed in the cache anymore */
2093         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2094
2095         RETURN(0);
2096 }
2097
2098 /**
2099  * Helper function for osd_xattr_set()
2100  */
2101 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2102                            const struct lu_buf *buf, const char *name, int fl)
2103 {
2104         struct osd_object      *obj      = osd_dt_obj(dt);
2105         struct inode           *inode    = obj->oo_inode;
2106         struct osd_thread_info *info     = osd_oti_get(env);
2107         struct dentry          *dentry   = &info->oti_child_dentry;
2108         int                     fs_flags = 0;
2109         int                     rc;
2110
2111         LASSERT(dt_object_exists(dt));
2112         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2113
2114         if (fl & LU_XATTR_REPLACE)
2115                 fs_flags |= XATTR_REPLACE;
2116
2117         if (fl & LU_XATTR_CREATE)
2118                 fs_flags |= XATTR_CREATE;
2119
2120         dentry->d_inode = inode;
2121         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2122                                    buf->lb_len, fs_flags);
2123         return rc;
2124 }
2125
2126 /**
2127  * Put the fid into lustre_mdt_attrs, and then place the structure
2128  * inode's ea. This fid should not be altered during the life time
2129  * of the inode.
2130  *
2131  * \retval +ve, on success
2132  * \retval -ve, on error
2133  *
2134  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2135  */
2136 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2137                           const struct lu_fid *fid)
2138 {
2139         struct osd_thread_info  *info      = osd_oti_get(env);
2140         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2141
2142         lustre_lma_init(mdt_attrs, fid);
2143         lustre_lma_swab(mdt_attrs);
2144         return __osd_xattr_set(env, dt,
2145                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2146                                XATTR_NAME_LMA, LU_XATTR_CREATE);
2147
2148 }
2149
2150 /**
2151  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2152  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2153  * To have compatilibility with 1.8 ldiskfs driver we need to have
2154  * magic number at start of fid data.
2155  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2156  * its inmemory API.
2157  */
2158 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2159                                   const struct dt_rec *fid)
2160 {
2161         param->edp_magic = LDISKFS_LUFID_MAGIC;
2162         param->edp_len =  sizeof(struct lu_fid) + 1;
2163
2164         fid_cpu_to_be((struct lu_fid *)param->edp_data,
2165                       (struct lu_fid *)fid);
2166 }
2167
2168 /**
2169  * Try to read the fid from inode ea into dt_rec, if return value
2170  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2171  *
2172  * \param fid object fid.
2173  *
2174  * \retval 0 on success
2175  */
2176 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2177                           __u32 ino, struct lu_fid *fid,
2178                           struct osd_inode_id *id)
2179 {
2180         struct osd_thread_info *info  = osd_oti_get(env);
2181         struct inode           *inode;
2182         ENTRY;
2183
2184         osd_id_gen(id, ino, OSD_OII_NOGEN);
2185         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2186         if (IS_ERR(inode))
2187                 RETURN(PTR_ERR(inode));
2188
2189         iput(inode);
2190         RETURN(0);
2191 }
2192
2193 /**
2194  * OSD layer object create function for interoperability mode (b11826).
2195  * This is mostly similar to osd_object_create(). Only difference being, fid is
2196  * inserted into inode ea here.
2197  *
2198  * \retval   0, on success
2199  * \retval -ve, on error
2200  */
2201 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2202                                 struct lu_attr *attr,
2203                                 struct dt_allocation_hint *hint,
2204                                 struct dt_object_format *dof,
2205                                 struct thandle *th)
2206 {
2207         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2208         struct osd_object      *obj    = osd_dt_obj(dt);
2209         struct osd_thread_info *info   = osd_oti_get(env);
2210         int                     result;
2211
2212         ENTRY;
2213
2214         LASSERT(osd_invariant(obj));
2215         LASSERT(!dt_object_exists(dt));
2216         LASSERT(osd_write_locked(env, obj));
2217         LASSERT(th != NULL);
2218
2219         if (unlikely(fid_is_acct(fid)))
2220                 /* Quota files can't be created from the kernel any more,
2221                  * 'tune2fs -O quota' will take care of creating them */
2222                 RETURN(-EPERM);
2223
2224         OSD_EXEC_OP(th, create);
2225
2226         result = __osd_object_create(info, obj, attr, hint, dof, th);
2227         /* objects under osd root shld have igif fid, so dont add fid EA */
2228         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2229                 result = osd_ea_fid_set(env, dt, fid);
2230
2231         if (result == 0)
2232                 result = __osd_oi_insert(env, obj, fid, th);
2233
2234         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2235         LINVRNT(osd_invariant(obj));
2236         RETURN(result);
2237 }
2238
2239 static int osd_declare_object_ref_add(const struct lu_env *env,
2240                                       struct dt_object *dt,
2241                                       struct thandle *handle)
2242 {
2243         struct osd_thandle *oh;
2244
2245         /* it's possible that object doesn't exist yet */
2246         LASSERT(handle != NULL);
2247
2248         oh = container_of0(handle, struct osd_thandle, ot_super);
2249         LASSERT(oh->ot_handle == NULL);
2250
2251         OSD_DECLARE_OP(oh, ref_add);
2252         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2253
2254         return 0;
2255 }
2256
2257 /*
2258  * Concurrency: @dt is write locked.
2259  */
2260 static int osd_object_ref_add(const struct lu_env *env,
2261                               struct dt_object *dt, struct thandle *th)
2262 {
2263         struct osd_object *obj = osd_dt_obj(dt);
2264         struct inode      *inode = obj->oo_inode;
2265
2266         LINVRNT(osd_invariant(obj));
2267         LASSERT(dt_object_exists(dt));
2268         LASSERT(osd_write_locked(env, obj));
2269         LASSERT(th != NULL);
2270
2271         OSD_EXEC_OP(th, ref_add);
2272
2273         /*
2274          * DIR_NLINK feature is set for compatibility reasons if:
2275          * 1) nlinks > LDISKFS_LINK_MAX, or
2276          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2277          *
2278          * It is easier to always set this flag (rather than check and set),
2279          * since it has less overhead, and the superblock will be dirtied
2280          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2281          * do not actually care whether this flag is set or not.
2282          */
2283         cfs_spin_lock(&obj->oo_guard);
2284         inode->i_nlink++;
2285         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2286                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2287                     inode->i_nlink == 2)
2288                         inode->i_nlink = 1;
2289         }
2290         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2291         cfs_spin_unlock(&obj->oo_guard);
2292         inode->i_sb->s_op->dirty_inode(inode);
2293         LINVRNT(osd_invariant(obj));
2294
2295         return 0;
2296 }
2297
2298 static int osd_declare_object_ref_del(const struct lu_env *env,
2299                                       struct dt_object *dt,
2300                                       struct thandle *handle)
2301 {
2302         struct osd_thandle *oh;
2303
2304         LASSERT(dt_object_exists(dt));
2305         LASSERT(handle != NULL);
2306
2307         oh = container_of0(handle, struct osd_thandle, ot_super);
2308         LASSERT(oh->ot_handle == NULL);
2309
2310         OSD_DECLARE_OP(oh, ref_del);
2311         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2312
2313         return 0;
2314 }
2315
2316 /*
2317  * Concurrency: @dt is write locked.
2318  */
2319 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2320                               struct thandle *th)
2321 {
2322         struct osd_object *obj = osd_dt_obj(dt);
2323         struct inode      *inode = obj->oo_inode;
2324
2325         LINVRNT(osd_invariant(obj));
2326         LASSERT(dt_object_exists(dt));
2327         LASSERT(osd_write_locked(env, obj));
2328         LASSERT(th != NULL);
2329
2330         OSD_EXEC_OP(th, ref_del);
2331
2332         cfs_spin_lock(&obj->oo_guard);
2333         LASSERT(inode->i_nlink > 0);
2334         inode->i_nlink--;
2335         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2336          * then the nlink count is 1. Don't let it be set to 0 or the directory
2337          * inode will be deleted incorrectly. */
2338         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2339                 inode->i_nlink++;
2340         cfs_spin_unlock(&obj->oo_guard);
2341         inode->i_sb->s_op->dirty_inode(inode);
2342         LINVRNT(osd_invariant(obj));
2343
2344         return 0;
2345 }
2346
2347 /*
2348  * Get the 64-bit version for an inode.
2349  */
2350 static int osd_object_version_get(const struct lu_env *env,
2351                                   struct dt_object *dt, dt_obj_version_t *ver)
2352 {
2353         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2354
2355         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2356                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2357         *ver = LDISKFS_I(inode)->i_fs_version;
2358         return 0;
2359 }
2360
2361 /*
2362  * Concurrency: @dt is read locked.
2363  */
2364 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2365                          struct lu_buf *buf, const char *name,
2366                          struct lustre_capa *capa)
2367 {
2368         struct osd_object      *obj    = osd_dt_obj(dt);
2369         struct inode           *inode  = obj->oo_inode;
2370         struct osd_thread_info *info   = osd_oti_get(env);
2371         struct dentry          *dentry = &info->oti_obj_dentry;
2372
2373         /* version get is not real XATTR but uses xattr API */
2374         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2375                 /* for version we are just using xattr API but change inode
2376                  * field instead */
2377                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2378                 osd_object_version_get(env, dt, buf->lb_buf);
2379                 return sizeof(dt_obj_version_t);
2380         }
2381
2382         LASSERT(dt_object_exists(dt));
2383         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2384         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2385
2386         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2387                 return -EACCES;
2388
2389         dentry->d_inode = inode;
2390         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2391 }
2392
2393
2394 static int osd_declare_xattr_set(const struct lu_env *env,
2395                                  struct dt_object *dt,
2396                                  const struct lu_buf *buf, const char *name,
2397                                  int fl, struct thandle *handle)
2398 {
2399         struct osd_thandle *oh;
2400
2401         LASSERT(handle != NULL);
2402
2403         oh = container_of0(handle, struct osd_thandle, ot_super);
2404         LASSERT(oh->ot_handle == NULL);
2405
2406         OSD_DECLARE_OP(oh, xattr_set);
2407         if (strcmp(name, XATTR_NAME_VERSION) == 0)
2408                 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2409         else
2410                 oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2411
2412         return 0;
2413 }
2414
2415 /*
2416  * Set the 64-bit version for object
2417  */
2418 static void osd_object_version_set(const struct lu_env *env,
2419                                    struct dt_object *dt,
2420                                    dt_obj_version_t *new_version)
2421 {
2422         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2423
2424         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2425                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2426
2427         LDISKFS_I(inode)->i_fs_version = *new_version;
2428         /** Version is set after all inode operations are finished,
2429          *  so we should mark it dirty here */
2430         inode->i_sb->s_op->dirty_inode(inode);
2431 }
2432
2433 /*
2434  * Concurrency: @dt is write locked.
2435  */
2436 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2437                          const struct lu_buf *buf, const char *name, int fl,
2438                          struct thandle *handle, struct lustre_capa *capa)
2439 {
2440         LASSERT(handle != NULL);
2441
2442         /* version set is not real XATTR */
2443         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2444                 /* for version we are just using xattr API but change inode
2445                  * field instead */
2446                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2447                 osd_object_version_set(env, dt, buf->lb_buf);
2448                 return sizeof(dt_obj_version_t);
2449         }
2450
2451         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2452                 return -EACCES;
2453
2454         OSD_EXEC_OP(handle, xattr_set);
2455         return __osd_xattr_set(env, dt, buf, name, fl);
2456 }
2457
2458 /*
2459  * Concurrency: @dt is read locked.
2460  */
2461 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2462                           struct lu_buf *buf, struct lustre_capa *capa)
2463 {
2464         struct osd_object      *obj    = osd_dt_obj(dt);
2465         struct inode           *inode  = obj->oo_inode;
2466         struct osd_thread_info *info   = osd_oti_get(env);
2467         struct dentry          *dentry = &info->oti_obj_dentry;
2468
2469         LASSERT(dt_object_exists(dt));
2470         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2471         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2472
2473         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2474                 return -EACCES;
2475
2476         dentry->d_inode = inode;
2477         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2478 }
2479
2480 static int osd_declare_xattr_del(const struct lu_env *env,
2481                                  struct dt_object *dt, const char *name,
2482                                  struct thandle *handle)
2483 {
2484         struct osd_thandle *oh;
2485
2486         LASSERT(dt_object_exists(dt));
2487         LASSERT(handle != NULL);
2488
2489         oh = container_of0(handle, struct osd_thandle, ot_super);
2490         LASSERT(oh->ot_handle == NULL);
2491
2492         OSD_DECLARE_OP(oh, xattr_set);
2493         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2494
2495         return 0;
2496 }
2497
2498 /*
2499  * Concurrency: @dt is write locked.
2500  */
2501 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2502                          const char *name, struct thandle *handle,
2503                          struct lustre_capa *capa)
2504 {
2505         struct osd_object      *obj    = osd_dt_obj(dt);
2506         struct inode           *inode  = obj->oo_inode;
2507         struct osd_thread_info *info   = osd_oti_get(env);
2508         struct dentry          *dentry = &info->oti_obj_dentry;
2509         int                     rc;
2510
2511         LASSERT(dt_object_exists(dt));
2512         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2513         LASSERT(osd_write_locked(env, obj));
2514         LASSERT(handle != NULL);
2515
2516         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2517                 return -EACCES;
2518
2519         OSD_EXEC_OP(handle, xattr_set);
2520
2521         dentry->d_inode = inode;
2522         rc = inode->i_op->removexattr(dentry, name);
2523         return rc;
2524 }
2525
2526 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2527                                      struct dt_object *dt,
2528                                      struct lustre_capa *old,
2529                                      __u64 opc)
2530 {
2531         struct osd_thread_info *info = osd_oti_get(env);
2532         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2533         struct osd_object *obj = osd_dt_obj(dt);
2534         struct osd_device *dev = osd_obj2dev(obj);
2535         struct lustre_capa_key *key = &info->oti_capa_key;
2536         struct lustre_capa *capa = &info->oti_capa;
2537         struct obd_capa *oc;
2538         struct md_capainfo *ci;
2539         int rc;
2540         ENTRY;
2541
2542         if (!dev->od_fl_capa)
2543                 RETURN(ERR_PTR(-ENOENT));
2544
2545         LASSERT(dt_object_exists(dt));
2546         LINVRNT(osd_invariant(obj));
2547
2548         /* renewal sanity check */
2549         if (old && osd_object_auth(env, dt, old, opc))
2550                 RETURN(ERR_PTR(-EACCES));
2551
2552         ci = md_capainfo(env);
2553         if (unlikely(!ci))
2554                 RETURN(ERR_PTR(-ENOENT));
2555
2556         switch (ci->mc_auth) {
2557         case LC_ID_NONE:
2558                 RETURN(NULL);
2559         case LC_ID_PLAIN:
2560                 capa->lc_uid = obj->oo_inode->i_uid;
2561                 capa->lc_gid = obj->oo_inode->i_gid;
2562                 capa->lc_flags = LC_ID_PLAIN;
2563                 break;
2564         case LC_ID_CONVERT: {
2565                 __u32 d[4], s[4];
2566
2567                 s[0] = obj->oo_inode->i_uid;
2568                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2569                 s[2] = obj->oo_inode->i_gid;
2570                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2571                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2572                 if (unlikely(rc))
2573                         RETURN(ERR_PTR(rc));
2574
2575                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2576                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2577                 capa->lc_flags = LC_ID_CONVERT;
2578                 break;
2579         }
2580         default:
2581                 RETURN(ERR_PTR(-EINVAL));
2582         }
2583
2584         capa->lc_fid = *fid;
2585         capa->lc_opc = opc;
2586         capa->lc_flags |= dev->od_capa_alg << 24;
2587         capa->lc_timeout = dev->od_capa_timeout;
2588         capa->lc_expiry = 0;
2589
2590         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2591         if (oc) {
2592                 LASSERT(!capa_is_expired(oc));
2593                 RETURN(oc);
2594         }
2595
2596         cfs_spin_lock(&capa_lock);
2597         *key = dev->od_capa_keys[1];
2598         cfs_spin_unlock(&capa_lock);
2599
2600         capa->lc_keyid = key->lk_keyid;
2601         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2602
2603         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2604         if (rc) {
2605                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2606                 RETURN(ERR_PTR(rc));
2607         }
2608
2609         oc = capa_add(dev->od_capa_hash, capa);
2610         RETURN(oc);
2611 }
2612
2613 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2614 {
2615         struct osd_object       *obj    = osd_dt_obj(dt);
2616         struct inode            *inode  = obj->oo_inode;
2617         struct osd_thread_info  *info   = osd_oti_get(env);
2618         struct dentry           *dentry = &info->oti_obj_dentry;
2619         struct file             *file   = &info->oti_file;
2620         int                     rc;
2621
2622         ENTRY;
2623
2624         dentry->d_inode = inode;
2625         file->f_dentry = dentry;
2626         file->f_mapping = inode->i_mapping;
2627         file->f_op = inode->i_fop;
2628         mutex_lock(&inode->i_mutex);
2629         rc = file->f_op->fsync(file, dentry, 0);
2630         mutex_unlock(&inode->i_mutex);
2631         RETURN(rc);
2632 }
2633
2634 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2635                         void **data)
2636 {
2637         struct osd_object *obj = osd_dt_obj(dt);
2638         ENTRY;
2639
2640         *data = (void *)obj->oo_inode;
2641         RETURN(0);
2642 }
2643
2644 /*
2645  * Index operations.
2646  */
2647
2648 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2649                            const struct dt_index_features *feat)
2650 {
2651         struct iam_descr *descr;
2652
2653         if (osd_object_is_root(o))
2654                 return feat == &dt_directory_features;
2655
2656         LASSERT(o->oo_dir != NULL);
2657
2658         descr = o->oo_dir->od_container.ic_descr;
2659         if (feat == &dt_directory_features) {
2660                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2661                         return 1;
2662                 else
2663                         return 0;
2664         } else {
2665                 return
2666                         feat->dif_keysize_min <= descr->id_key_size &&
2667                         descr->id_key_size <= feat->dif_keysize_max &&
2668                         feat->dif_recsize_min <= descr->id_rec_size &&
2669                         descr->id_rec_size <= feat->dif_recsize_max &&
2670                         !(feat->dif_flags & (DT_IND_VARKEY |
2671                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2672                         ergo(feat->dif_flags & DT_IND_UPDATE,
2673                              1 /* XXX check that object (and file system) is
2674                                 * writable */);
2675         }
2676 }
2677
2678 static int osd_iam_container_init(const struct lu_env *env,
2679                                   struct osd_object *obj,
2680                                   struct osd_directory *dir)
2681 {
2682         struct iam_container *bag = &dir->od_container;
2683         int result;
2684
2685         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2686         if (result != 0)
2687                 return result;
2688
2689         result = iam_container_setup(bag);
2690         if (result == 0)
2691                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2692         else
2693                 iam_container_fini(bag);
2694
2695         return result;
2696 }
2697
2698
2699 /*
2700  * Concurrency: no external locking is necessary.
2701  */
2702 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2703                          const struct dt_index_features *feat)
2704 {
2705         int                      result;
2706         int                      skip_iam = 0;
2707         struct osd_object       *obj = osd_dt_obj(dt);
2708         struct osd_device       *osd = osd_obj2dev(obj);
2709
2710         LINVRNT(osd_invariant(obj));
2711         LASSERT(dt_object_exists(dt));
2712
2713         if (osd_object_is_root(obj)) {
2714                 dt->do_index_ops = &osd_index_ea_ops;
2715                 result = 0;
2716         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2717                 dt->do_index_ops = &osd_index_ea_ops;
2718                 if (S_ISDIR(obj->oo_inode->i_mode))
2719                         result = 0;
2720                 else
2721                         result = -ENOTDIR;
2722                 skip_iam = 1;
2723         } else if (unlikely(feat == &dt_otable_features)) {
2724                 dt->do_index_ops = &osd_otable_ops;
2725                 return 0;
2726         } else if (feat == &dt_acct_features) {
2727                 dt->do_index_ops = &osd_acct_index_ops;
2728                 result = 0;
2729                 skip_iam = 1;
2730         } else if (!osd_has_index(obj)) {
2731                 struct osd_directory *dir;
2732
2733                 OBD_ALLOC_PTR(dir);
2734                 if (dir != NULL) {
2735
2736                         cfs_spin_lock(&obj->oo_guard);
2737                         if (obj->oo_dir == NULL)
2738                                 obj->oo_dir = dir;
2739                         else
2740                                 /*
2741                                  * Concurrent thread allocated container data.
2742                                  */
2743                                 OBD_FREE_PTR(dir);
2744                         cfs_spin_unlock(&obj->oo_guard);
2745                         /*
2746                          * Now, that we have container data, serialize its
2747                          * initialization.
2748                          */
2749                         cfs_down_write(&obj->oo_ext_idx_sem);
2750                         /*
2751                          * recheck under lock.
2752                          */
2753                         if (!osd_has_index(obj))
2754                                 result = osd_iam_container_init(env, obj, dir);
2755                         else
2756                                 result = 0;
2757                         cfs_up_write(&obj->oo_ext_idx_sem);
2758                 } else {
2759                         result = -ENOMEM;
2760                 }
2761         } else {
2762                 result = 0;
2763         }
2764
2765         if (result == 0 && skip_iam == 0) {
2766                 if (!osd_iam_index_probe(env, obj, feat))
2767                         result = -ENOTDIR;
2768         }
2769         LINVRNT(osd_invariant(obj));
2770
2771         return result;
2772 }
2773
2774 static int osd_otable_it_attr_get(const struct lu_env *env,
2775                                  struct dt_object *dt,
2776                                  struct lu_attr *attr,
2777                                  struct lustre_capa *capa)
2778 {
2779         attr->la_valid = 0;
2780         return 0;
2781 }
2782
2783 static const struct dt_object_operations osd_obj_ops = {
2784         .do_read_lock         = osd_object_read_lock,
2785         .do_write_lock        = osd_object_write_lock,
2786         .do_read_unlock       = osd_object_read_unlock,
2787         .do_write_unlock      = osd_object_write_unlock,
2788         .do_write_locked      = osd_object_write_locked,
2789         .do_attr_get          = osd_attr_get,
2790         .do_declare_attr_set  = osd_declare_attr_set,
2791         .do_attr_set          = osd_attr_set,
2792         .do_ah_init           = osd_ah_init,
2793         .do_declare_create    = osd_declare_object_create,
2794         .do_create            = osd_object_create,
2795         .do_declare_destroy   = osd_declare_object_destroy,
2796         .do_destroy           = osd_object_destroy,
2797         .do_index_try         = osd_index_try,
2798         .do_declare_ref_add   = osd_declare_object_ref_add,
2799         .do_ref_add           = osd_object_ref_add,
2800         .do_declare_ref_del   = osd_declare_object_ref_del,
2801         .do_ref_del           = osd_object_ref_del,
2802         .do_xattr_get         = osd_xattr_get,
2803         .do_declare_xattr_set = osd_declare_xattr_set,
2804         .do_xattr_set         = osd_xattr_set,
2805         .do_declare_xattr_del = osd_declare_xattr_del,
2806         .do_xattr_del         = osd_xattr_del,
2807         .do_xattr_list        = osd_xattr_list,
2808         .do_capa_get          = osd_capa_get,
2809         .do_object_sync       = osd_object_sync,
2810         .do_data_get          = osd_data_get,
2811 };
2812
2813 /**
2814  * dt_object_operations for interoperability mode
2815  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2816  */
2817 static const struct dt_object_operations osd_obj_ea_ops = {
2818         .do_read_lock         = osd_object_read_lock,
2819         .do_write_lock        = osd_object_write_lock,
2820         .do_read_unlock       = osd_object_read_unlock,
2821         .do_write_unlock      = osd_object_write_unlock,
2822         .do_write_locked      = osd_object_write_locked,
2823         .do_attr_get          = osd_attr_get,
2824         .do_declare_attr_set  = osd_declare_attr_set,
2825         .do_attr_set          = osd_attr_set,
2826         .do_ah_init           = osd_ah_init,
2827         .do_declare_create    = osd_declare_object_create,
2828         .do_create            = osd_object_ea_create,
2829         .do_declare_destroy   = osd_declare_object_destroy,
2830         .do_destroy           = osd_object_destroy,
2831         .do_index_try         = osd_index_try,
2832         .do_declare_ref_add   = osd_declare_object_ref_add,
2833         .do_ref_add           = osd_object_ref_add,
2834         .do_declare_ref_del   = osd_declare_object_ref_del,
2835         .do_ref_del           = osd_object_ref_del,
2836         .do_xattr_get         = osd_xattr_get,
2837         .do_declare_xattr_set = osd_declare_xattr_set,
2838         .do_xattr_set         = osd_xattr_set,
2839         .do_declare_xattr_del = osd_declare_xattr_del,
2840         .do_xattr_del         = osd_xattr_del,
2841         .do_xattr_list        = osd_xattr_list,
2842         .do_capa_get          = osd_capa_get,
2843         .do_object_sync       = osd_object_sync,
2844         .do_data_get          = osd_data_get,
2845 };
2846
2847 static const struct dt_object_operations osd_obj_otable_it_ops = {
2848         .do_attr_get    = osd_otable_it_attr_get,
2849         .do_index_try   = osd_index_try,
2850 };
2851
2852 static int osd_index_declare_iam_delete(const struct lu_env *env,
2853                                         struct dt_object *dt,
2854                                         const struct dt_key *key,
2855                                         struct thandle *handle)
2856 {
2857         struct osd_thandle    *oh;
2858
2859         oh = container_of0(handle, struct osd_thandle, ot_super);
2860         LASSERT(oh->ot_handle == NULL);
2861
2862         OSD_DECLARE_OP(oh, delete);
2863         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2864
2865         return 0;
2866 }
2867
2868 /**
2869  *      delete a (key, value) pair from index \a dt specified by \a key
2870  *
2871  *      \param  dt      osd index object
2872  *      \param  key     key for index
2873  *      \param  rec     record reference
2874  *      \param  handle  transaction handler
2875  *
2876  *      \retval  0  success
2877  *      \retval -ve   failure
2878  */
2879
2880 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
2881                                 const struct dt_key *key,
2882                                 struct thandle *handle,
2883                                 struct lustre_capa *capa)
2884 {
2885         struct osd_object     *obj = osd_dt_obj(dt);
2886         struct osd_thandle    *oh;
2887         struct iam_path_descr *ipd;
2888         struct iam_container  *bag = &obj->oo_dir->od_container;
2889         int                    rc;
2890
2891         ENTRY;
2892
2893         LINVRNT(osd_invariant(obj));
2894         LASSERT(dt_object_exists(dt));
2895         LASSERT(bag->ic_object == obj->oo_inode);
2896         LASSERT(handle != NULL);
2897
2898         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2899                 RETURN(-EACCES);
2900
2901         OSD_EXEC_OP(handle, delete);
2902
2903         ipd = osd_idx_ipd_get(env, bag);
2904         if (unlikely(ipd == NULL))
2905                 RETURN(-ENOMEM);
2906
2907         oh = container_of0(handle, struct osd_thandle, ot_super);
2908         LASSERT(oh->ot_handle != NULL);
2909         LASSERT(oh->ot_handle->h_transaction != NULL);
2910
2911         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
2912         osd_ipd_put(env, bag, ipd);
2913         LINVRNT(osd_invariant(obj));
2914         RETURN(rc);
2915 }
2916
2917 static int osd_index_declare_ea_delete(const struct lu_env *env,
2918                                        struct dt_object *dt,
2919                                        const struct dt_key *key,
2920                                        struct thandle *handle)
2921 {
2922         struct osd_thandle *oh;
2923
2924         LASSERT(dt_object_exists(dt));
2925         LASSERT(handle != NULL);
2926
2927         oh = container_of0(handle, struct osd_thandle, ot_super);
2928         LASSERT(oh->ot_handle == NULL);
2929
2930         OSD_DECLARE_OP(oh, delete);
2931         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2932
2933         LASSERT(osd_dt_obj(dt)->oo_inode);
2934         osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
2935                         osd_dt_obj(dt)->oo_inode);
2936         osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
2937                         osd_dt_obj(dt)->oo_inode);
2938
2939         return 0;
2940 }
2941
2942 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
2943                                           struct dt_rec *fid)
2944 {
2945         struct osd_fid_pack *rec;
2946         int                  rc = -ENODATA;
2947
2948         if (de->file_type & LDISKFS_DIRENT_LUFID) {
2949                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
2950                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
2951         }
2952         RETURN(rc);
2953 }
2954
2955 /**
2956  * Index delete function for interoperability mode (b11826).
2957  * It will remove the directory entry added by osd_index_ea_insert().
2958  * This entry is needed to maintain name->fid mapping.
2959  *
2960  * \param key,  key i.e. file entry to be deleted
2961  *
2962  * \retval   0, on success
2963  * \retval -ve, on error
2964  */
2965 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
2966                                const struct dt_key *key,
2967                                struct thandle *handle,
2968                                struct lustre_capa *capa)
2969 {
2970         struct osd_object          *obj    = osd_dt_obj(dt);
2971         struct inode               *dir    = obj->oo_inode;
2972         struct dentry              *dentry;
2973         struct osd_thandle         *oh;
2974         struct ldiskfs_dir_entry_2 *de;
2975         struct buffer_head         *bh;
2976         struct htree_lock          *hlock = NULL;
2977         int                         rc;
2978
2979         ENTRY;
2980
2981         LINVRNT(osd_invariant(obj));
2982         LASSERT(dt_object_exists(dt));
2983         LASSERT(handle != NULL);
2984
2985         OSD_EXEC_OP(handle, delete);
2986
2987         oh = container_of(handle, struct osd_thandle, ot_super);
2988         LASSERT(oh->ot_handle != NULL);
2989         LASSERT(oh->ot_handle->h_transaction != NULL);
2990
2991         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
2992                 RETURN(-EACCES);
2993
2994         dentry = osd_child_dentry_get(env, obj,
2995                                       (char *)key, strlen((char *)key));
2996
2997         if (obj->oo_hl_head != NULL) {
2998                 hlock = osd_oti_get(env)->oti_hlock;
2999                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3000                                    dir, LDISKFS_HLOCK_DEL);
3001         } else {
3002                 cfs_down_write(&obj->oo_ext_idx_sem);
3003         }
3004
3005         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3006         if (bh) {
3007                 rc = ldiskfs_delete_entry(oh->ot_handle,
3008                                           dir, de, bh);
3009                 brelse(bh);
3010         } else {
3011                 rc = -ENOENT;
3012         }
3013         if (hlock != NULL)
3014                 ldiskfs_htree_unlock(hlock);
3015         else
3016                 cfs_up_write(&obj->oo_ext_idx_sem);
3017
3018         LASSERT(osd_invariant(obj));
3019         RETURN(rc);
3020 }
3021
3022 /**
3023  *      Lookup index for \a key and copy record to \a rec.
3024  *
3025  *      \param  dt      osd index object
3026  *      \param  key     key for index
3027  *      \param  rec     record reference
3028  *
3029  *      \retval  +ve  success : exact mach
3030  *      \retval  0    return record with key not greater than \a key
3031  *      \retval -ve   failure
3032  */
3033 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3034                                 struct dt_rec *rec, const struct dt_key *key,
3035                                 struct lustre_capa *capa)
3036 {
3037         struct osd_object      *obj = osd_dt_obj(dt);
3038         struct iam_path_descr  *ipd;
3039         struct iam_container   *bag = &obj->oo_dir->od_container;
3040         struct osd_thread_info *oti = osd_oti_get(env);
3041         struct iam_iterator    *it = &oti->oti_idx_it;
3042         struct iam_rec         *iam_rec;
3043         int                     rc;
3044
3045         ENTRY;
3046
3047         LASSERT(osd_invariant(obj));
3048         LASSERT(dt_object_exists(dt));
3049         LASSERT(bag->ic_object == obj->oo_inode);
3050
3051         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3052                 RETURN(-EACCES);
3053
3054         ipd = osd_idx_ipd_get(env, bag);
3055         if (IS_ERR(ipd))
3056                 RETURN(-ENOMEM);
3057
3058         /* got ipd now we can start iterator. */
3059         iam_it_init(it, bag, 0, ipd);
3060
3061         rc = iam_it_get(it, (struct iam_key *)key);
3062         if (rc >= 0) {
3063                 if (S_ISDIR(obj->oo_inode->i_mode))
3064                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3065                 else
3066                         iam_rec = (struct iam_rec *) rec;
3067
3068                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3069                 if (S_ISDIR(obj->oo_inode->i_mode))
3070                         osd_fid_unpack((struct lu_fid *) rec,
3071                                        (struct osd_fid_pack *)iam_rec);
3072         }
3073         iam_it_put(it);
3074         iam_it_fini(it);
3075         osd_ipd_put(env, bag, ipd);
3076
3077         LINVRNT(osd_invariant(obj));
3078
3079         RETURN(rc);
3080 }
3081
3082 static int osd_index_declare_iam_insert(const struct lu_env *env,
3083                                         struct dt_object *dt,
3084                                         const struct dt_rec *rec,
3085                                         const struct dt_key *key,
3086                                         struct thandle *handle)
3087 {
3088         struct osd_thandle *oh;
3089
3090         LASSERT(dt_object_exists(dt));
3091         LASSERT(handle != NULL);
3092
3093         oh = container_of0(handle, struct osd_thandle, ot_super);
3094         LASSERT(oh->ot_handle == NULL);
3095
3096         OSD_DECLARE_OP(oh, insert);
3097         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3098
3099         return 0;
3100 }
3101
3102 /**
3103  *      Inserts (key, value) pair in \a dt index object.
3104  *
3105  *      \param  dt      osd index object
3106  *      \param  key     key for index
3107  *      \param  rec     record reference
3108  *      \param  th      transaction handler
3109  *
3110  *      \retval  0  success
3111  *      \retval -ve failure
3112  */
3113 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3114                                 const struct dt_rec *rec,
3115                                 const struct dt_key *key, struct thandle *th,
3116                                 struct lustre_capa *capa, int ignore_quota)
3117 {
3118         struct osd_object     *obj = osd_dt_obj(dt);
3119         struct iam_path_descr *ipd;
3120         struct osd_thandle    *oh;
3121         struct iam_container  *bag = &obj->oo_dir->od_container;
3122 #ifdef HAVE_QUOTA_SUPPORT
3123         cfs_cap_t              save = cfs_curproc_cap_pack();
3124 #endif
3125         struct osd_thread_info *oti = osd_oti_get(env);
3126         struct iam_rec         *iam_rec = (struct iam_rec *)oti->oti_ldp;
3127         int                     rc;
3128
3129         ENTRY;
3130
3131         LINVRNT(osd_invariant(obj));
3132         LASSERT(dt_object_exists(dt));
3133         LASSERT(bag->ic_object == obj->oo_inode);
3134         LASSERT(th != NULL);
3135
3136         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3137                 RETURN(-EACCES);
3138
3139         OSD_EXEC_OP(th, insert);
3140
3141         ipd = osd_idx_ipd_get(env, bag);
3142         if (unlikely(ipd == NULL))
3143                 RETURN(-ENOMEM);
3144
3145         oh = container_of0(th, struct osd_thandle, ot_super);
3146         LASSERT(oh->ot_handle != NULL);
3147         LASSERT(oh->ot_handle->h_transaction != NULL);
3148 #ifdef HAVE_QUOTA_SUPPORT
3149         if (ignore_quota)
3150                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3151         else
3152                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3153 #endif
3154         if (S_ISDIR(obj->oo_inode->i_mode))
3155                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3156         else
3157                 iam_rec = (struct iam_rec *) rec;
3158         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3159                         iam_rec, ipd);
3160 #ifdef HAVE_QUOTA_SUPPORT
3161         cfs_curproc_cap_unpack(save);
3162 #endif
3163         osd_ipd_put(env, bag, ipd);
3164         LINVRNT(osd_invariant(obj));
3165         RETURN(rc);
3166 }
3167
3168 /**
3169  * Calls ldiskfs_add_entry() to add directory entry
3170  * into the directory. This is required for
3171  * interoperability mode (b11826)
3172  *
3173  * \retval   0, on success
3174  * \retval -ve, on error
3175  */
3176 static int __osd_ea_add_rec(struct osd_thread_info *info,
3177                             struct osd_object *pobj, struct inode  *cinode,
3178                             const char *name, const struct dt_rec *fid,
3179                             struct htree_lock *hlock, struct thandle *th)
3180 {
3181         struct ldiskfs_dentry_param *ldp;
3182         struct dentry               *child;
3183         struct osd_thandle          *oth;
3184         int                          rc;
3185
3186         oth = container_of(th, struct osd_thandle, ot_super);
3187         LASSERT(oth->ot_handle != NULL);
3188         LASSERT(oth->ot_handle->h_transaction != NULL);
3189
3190         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3191
3192         /* XXX: remove fid_is_igif() check here.
3193          * IGIF check is just to handle insertion of .. when it is 'ROOT',
3194          * it is IGIF now but needs FID in dir entry as well for readdir
3195          * to work.
3196          * LU-838 should fix that and remove fid_is_igif() check */
3197         if (fid_is_igif((struct lu_fid *)fid) ||
3198             fid_is_norm((struct lu_fid *)fid)) {
3199                 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3200                 osd_get_ldiskfs_dirent_param(ldp, fid);
3201                 child->d_fsdata = (void *)ldp;
3202         } else {
3203                 child->d_fsdata = NULL;
3204         }
3205         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3206
3207         RETURN(rc);
3208 }
3209
3210 /**
3211  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3212  * into the directory.Also sets flags into osd object to
3213  * indicate dot and dotdot are created. This is required for
3214  * interoperability mode (b11826)
3215  *
3216  * \param dir   directory for dot and dotdot fixup.
3217  * \param obj   child object for linking
3218  *
3219  * \retval   0, on success
3220  * \retval -ve, on error
3221  */
3222 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3223                               struct osd_object *dir,
3224                               struct inode  *parent_dir, const char *name,
3225                               const struct dt_rec *dot_fid,
3226                               const struct dt_rec *dot_dot_fid,
3227                               struct thandle *th)
3228 {
3229         struct inode                *inode = dir->oo_inode;
3230         struct ldiskfs_dentry_param *dot_ldp;
3231         struct ldiskfs_dentry_param *dot_dot_ldp;
3232         struct osd_thandle          *oth;
3233         int result = 0;
3234
3235         oth = container_of(th, struct osd_thandle, ot_super);
3236         LASSERT(oth->ot_handle->h_transaction != NULL);
3237         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3238
3239         if (strcmp(name, dot) == 0) {
3240                 if (dir->oo_compat_dot_created) {
3241                         result = -EEXIST;
3242                 } else {
3243                         LASSERT(inode == parent_dir);
3244                         dir->oo_compat_dot_created = 1;
3245                         result = 0;
3246                 }
3247         } else if(strcmp(name, dotdot) == 0) {
3248                 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3249                 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;