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