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