Whamcloud - gitweb
LU-2158 lvfs: remove the fsfilt and lvfs layer
[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, 2013, Intel Corporation.
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
57 /*
58  * struct OBD_{ALLOC,FREE}*()
59  * OBD_FAIL_CHECK
60  */
61 #include <obd_support.h>
62 /* struct ptlrpc_thread */
63 #include <lustre_net.h>
64 #include <lustre_fid.h>
65
66 #include "osd_internal.h"
67 #include "osd_dynlocks.h"
68
69 /* llo_* api support */
70 #include <md_object.h>
71 #include <lustre_quota.h>
72
73 int ldiskfs_pdo = 1;
74 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
75                 "ldiskfs with parallel directory operations");
76
77 int ldiskfs_track_declares_assert;
78 CFS_MODULE_PARM(ldiskfs_track_declares_assert, "i", int, 0644,
79                 "LBUG during tracking of declares");
80
81 /* Slab to allocate dynlocks */
82 struct kmem_cache *dynlock_cachep;
83
84 static struct lu_kmem_descr ldiskfs_caches[] = {
85         {
86                 .ckd_cache = &dynlock_cachep,
87                 .ckd_name  = "dynlock_cache",
88                 .ckd_size  = sizeof(struct dynlock_handle)
89         },
90         {
91                 .ckd_cache = NULL
92         }
93 };
94
95 static const char dot[] = ".";
96 static const char dotdot[] = "..";
97 static const char remote_obj_dir[] = "REM_OBJ_DIR";
98
99 static const struct lu_object_operations      osd_lu_obj_ops;
100 static const struct dt_object_operations      osd_obj_ops;
101 static const struct dt_object_operations      osd_obj_ea_ops;
102 static const struct dt_object_operations      osd_obj_otable_it_ops;
103 static const struct dt_index_operations       osd_index_iam_ops;
104 static const struct dt_index_operations       osd_index_ea_ops;
105
106 int osd_trans_declare_op2rb[] = {
107         [OSD_OT_ATTR_SET]       = OSD_OT_ATTR_SET,
108         [OSD_OT_PUNCH]          = OSD_OT_MAX,
109         [OSD_OT_XATTR_SET]      = OSD_OT_XATTR_SET,
110         [OSD_OT_CREATE]         = OSD_OT_DESTROY,
111         [OSD_OT_DESTROY]        = OSD_OT_CREATE,
112         [OSD_OT_REF_ADD]        = OSD_OT_REF_DEL,
113         [OSD_OT_REF_DEL]        = OSD_OT_REF_ADD,
114         [OSD_OT_WRITE]          = OSD_OT_WRITE,
115         [OSD_OT_INSERT]         = OSD_OT_DELETE,
116         [OSD_OT_DELETE]         = OSD_OT_INSERT,
117         [OSD_OT_UPDATE]         = OSD_OT_MAX,
118         [OSD_OT_QUOTA]          = OSD_OT_MAX,
119 };
120
121 static int osd_has_index(const struct osd_object *obj)
122 {
123         return obj->oo_dt.do_index_ops != NULL;
124 }
125
126 static int osd_object_invariant(const struct lu_object *l)
127 {
128         return osd_invariant(osd_obj(l));
129 }
130
131 /*
132  * Concurrency: doesn't matter
133  */
134 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
135 {
136         return osd_oti_get(env)->oti_r_locks > 0;
137 }
138
139 /*
140  * Concurrency: doesn't matter
141  */
142 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
143 {
144         struct osd_thread_info *oti = osd_oti_get(env);
145         return oti->oti_w_locks > 0 && o->oo_owner == env;
146 }
147
148 /*
149  * Concurrency: doesn't access mutable data
150  */
151 static int osd_root_get(const struct lu_env *env,
152                         struct dt_device *dev, struct lu_fid *f)
153 {
154         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
155         return 0;
156 }
157
158 /*
159  * OSD object methods.
160  */
161
162 /*
163  * Concurrency: no concurrent access is possible that early in object
164  * life-cycle.
165  */
166 static struct lu_object *osd_object_alloc(const struct lu_env *env,
167                                           const struct lu_object_header *hdr,
168                                           struct lu_device *d)
169 {
170         struct osd_object *mo;
171
172         OBD_ALLOC_PTR(mo);
173         if (mo != NULL) {
174                 struct lu_object *l;
175
176                 l = &mo->oo_dt.do_lu;
177                 dt_object_init(&mo->oo_dt, NULL, d);
178                 mo->oo_dt.do_ops = &osd_obj_ea_ops;
179                 l->lo_ops = &osd_lu_obj_ops;
180                 init_rwsem(&mo->oo_sem);
181                 init_rwsem(&mo->oo_ext_idx_sem);
182                 spin_lock_init(&mo->oo_guard);
183                 return l;
184         } else {
185                 return NULL;
186         }
187 }
188
189 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
190                 struct dentry *dentry, struct lustre_mdt_attrs *lma)
191 {
192         int rc;
193
194         CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
195         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
196                              info->oti_mdt_attrs_old, LMA_OLD_SIZE);
197         if (rc > 0) {
198                 if ((void *)lma != (void *)info->oti_mdt_attrs_old)
199                         memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
200                 rc = 0;
201                 lustre_lma_swab(lma);
202                 /* Check LMA compatibility */
203                 if (lma->lma_incompat & ~LMA_INCOMPAT_SUPP) {
204                         CWARN("%.16s: unsupported incompat LMA feature(s) %#x "
205                               "for fid = "DFID", ino = %lu\n",
206                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
207                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
208                               PFID(&lma->lma_self_fid), inode->i_ino);
209                         rc = -EOPNOTSUPP;
210                 }
211         } else if (rc == 0) {
212                 rc = -ENODATA;
213         }
214
215         return rc;
216 }
217
218 /*
219  * retrieve object from backend ext fs.
220  **/
221 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
222                        struct osd_inode_id *id)
223 {
224         struct inode *inode = NULL;
225
226         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
227         if (IS_ERR(inode)) {
228                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
229                        id->oii_ino, PTR_ERR(inode));
230         } else if (id->oii_gen != OSD_OII_NOGEN &&
231                    inode->i_generation != id->oii_gen) {
232                 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
233                        "gen1 = %u\n",
234                        id->oii_ino, id->oii_gen, inode->i_generation);
235                 iput(inode);
236                 inode = ERR_PTR(-ESTALE);
237         } else if (inode->i_nlink == 0) {
238                 /* due to parallel readdir and unlink,
239                 * we can have dead inode here. */
240                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
241                 make_bad_inode(inode);
242                 iput(inode);
243                 inode = ERR_PTR(-ESTALE);
244         } else if (is_bad_inode(inode)) {
245                 CWARN("%.16s: bad inode: ino = %u\n",
246                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
247                 iput(inode);
248                 inode = ERR_PTR(-ENOENT);
249         } else {
250                 if (id->oii_gen == OSD_OII_NOGEN)
251                         osd_id_gen(id, inode->i_ino, inode->i_generation);
252
253                 /* Do not update file c/mtime in ldiskfs.
254                  * NB: we don't have any lock to protect this because we don't
255                  * have reference on osd_object now, but contention with
256                  * another lookup + attr_set can't happen in the tiny window
257                  * between if (...) and set S_NOCMTIME. */
258                 if (!(inode->i_flags & S_NOCMTIME))
259                         inode->i_flags |= S_NOCMTIME;
260         }
261         return inode;
262 }
263
264 static struct inode *
265 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
266              struct osd_inode_id *id, struct lu_fid *fid)
267 {
268         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
269         struct inode            *inode;
270         int                      rc;
271
272         inode = osd_iget(info, dev, id);
273         if (IS_ERR(inode))
274                 return inode;
275
276         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
277         if (rc == 0) {
278                 *fid = lma->lma_self_fid;
279         } else if (rc == -ENODATA) {
280                 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
281                         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
282                 else
283                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
284         } else {
285                 iput(inode);
286                 inode = ERR_PTR(rc);
287         }
288         return inode;
289 }
290
291 /**
292  * \retval +v: new filter_fid, does not contain self-fid
293  * \retval 0:  filter_fid_old, contains self-fid
294  * \retval -v: other failure cases
295  */
296 int osd_get_idif(struct osd_thread_info *info, struct inode *inode,
297                  struct dentry *dentry, struct lu_fid *fid)
298 {
299         struct filter_fid_old   *ff     = &info->oti_ff;
300         struct ost_id           *ostid  = &info->oti_ostid;
301         int                      rc;
302
303         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
304         if (rc == sizeof(*ff)) {
305                 rc = 0;
306                 ostid_set_seq(ostid, le64_to_cpu(ff->ff_seq));
307                 ostid_set_id(ostid, le64_to_cpu(ff->ff_objid));
308                 /* XXX: should use real OST index in the future. LU-3569 */
309                 ostid_to_fid(fid, ostid, 0);
310         } else if (rc == sizeof(struct filter_fid)) {
311                 rc = 1;
312         } else if (rc >= 0) {
313                 rc = -EINVAL;
314         }
315
316         return rc;
317 }
318
319 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
320 {
321         struct osd_thread_info  *info   = osd_oti_get(env);
322         struct lustre_mdt_attrs *lma    = &info->oti_mdt_attrs;
323         struct inode            *inode  = obj->oo_inode;
324         struct dentry           *dentry = &info->oti_obj_dentry;
325         struct lu_fid           *fid    = NULL;
326         int                      rc;
327         ENTRY;
328
329         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
330                 RETURN(0);
331
332         CLASSERT(LMA_OLD_SIZE >= sizeof(*lma));
333         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA,
334                              info->oti_mdt_attrs_old, LMA_OLD_SIZE);
335         if (rc == -ENODATA && !fid_is_igif(lu_object_fid(&obj->oo_dt.do_lu)) &&
336             osd_obj2dev(obj)->od_check_ff) {
337                 fid = &lma->lma_self_fid;
338                 rc = osd_get_idif(info, inode, dentry, fid);
339                 if (rc > 0)
340                         RETURN(0);
341         }
342
343         if (unlikely(rc == -ENODATA))
344                 RETURN(0);
345
346         if (rc < 0)
347                 RETURN(rc);
348
349         if (rc > 0) {
350                 rc = 0;
351                 lustre_lma_swab(lma);
352                 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
353                              CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
354                         CWARN("%s: unsupported incompat LMA feature(s) %#x for "
355                               "fid = "DFID", ino = %lu\n",
356                               osd_obj2dev(obj)->od_svname,
357                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
358                               PFID(lu_object_fid(&obj->oo_dt.do_lu)),
359                               inode->i_ino);
360                         rc = -EOPNOTSUPP;
361                 } else if (!(lma->lma_compat & LMAC_NOT_IN_OI)) {
362                         fid = &lma->lma_self_fid;
363                 }
364         }
365
366         if (fid != NULL &&
367             unlikely(!lu_fid_eq(lu_object_fid(&obj->oo_dt.do_lu), fid))) {
368                 CDEBUG(D_INODE, "%s: FID "DFID" != self_fid "DFID"\n",
369                        osd_obj2dev(obj)->od_svname,
370                        PFID(lu_object_fid(&obj->oo_dt.do_lu)),
371                        PFID(&lma->lma_self_fid));
372                 rc = -EREMCHG;
373         }
374
375         RETURN(rc);
376 }
377
378 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
379                           const struct lu_fid *fid,
380                           const struct lu_object_conf *conf)
381 {
382         struct osd_thread_info *info;
383         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
384         struct osd_device      *dev;
385         struct osd_idmap_cache *oic;
386         struct osd_inode_id    *id;
387         struct inode           *inode;
388         struct osd_scrub       *scrub;
389         struct scrub_file      *sf;
390         int                     result;
391         int                     saved  = 0;
392         bool                    in_oi  = false;
393         bool                    triggered = false;
394         ENTRY;
395
396         LINVRNT(osd_invariant(obj));
397         LASSERT(obj->oo_inode == NULL);
398         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
399
400         dev = osd_dev(ldev);
401         scrub = &dev->od_scrub;
402         sf = &scrub->os_file;
403         info = osd_oti_get(env);
404         LASSERT(info);
405         oic = &info->oti_cache;
406
407         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
408                 RETURN(-ENOENT);
409
410         /* For the object is created as locking anchor, or for the object to
411          * be created on disk. No need to osd_oi_lookup() at here because FID
412          * shouldn't never be re-used, if it's really a duplicate FID from
413          * unexpected reason, we should be able to detect it later by calling
414          * do_create->osd_oi_insert(). */
415         if (conf != NULL && conf->loc_flags & LOC_F_NEW)
416                 GOTO(out, result = 0);
417
418         /* Search order: 1. per-thread cache. */
419         if (lu_fid_eq(fid, &oic->oic_fid) &&
420             likely(oic->oic_dev == dev)) {
421                 id = &oic->oic_lid;
422                 goto iget;
423         }
424
425         id = &info->oti_id;
426         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
427                 /* Search order: 2. OI scrub pending list. */
428                 result = osd_oii_lookup(dev, fid, id);
429                 if (result == 0)
430                         goto iget;
431         }
432
433         /* Search order: 3. OI files. */
434         result = osd_oi_lookup(info, dev, fid, id, OI_CHECK_FLD);
435         if (result == -ENOENT) {
436                 if (!fid_is_norm(fid) ||
437                     fid_is_on_ost(info, dev, fid, OI_CHECK_FLD) ||
438                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
439                                       sf->sf_oi_bitmap))
440                         GOTO(out, result = 0);
441
442                 goto trigger;
443         }
444
445         if (result != 0)
446                 GOTO(out, result);
447
448         in_oi = true;
449
450 iget:
451         inode = osd_iget(info, dev, id);
452         if (IS_ERR(inode)) {
453                 result = PTR_ERR(inode);
454                 if (result == -ENOENT || result == -ESTALE) {
455                         if (!in_oi) {
456                                 fid_zero(&oic->oic_fid);
457                                 GOTO(out, result = -ENOENT);
458                         }
459
460                         /* XXX: There are three possible cases:
461                          *      1. Backup/restore caused the OI invalid.
462                          *      2. Someone unlinked the object but NOT removed
463                          *         the OI mapping, such as mount target device
464                          *         as ldiskfs, and modify something directly.
465                          *      3. Someone just removed the object between the
466                          *         former oi_lookup and the iget. It is normal.
467                          *
468                          *      It is diffcult to distinguish the 2nd from the
469                          *      1st case. Relatively speaking, the 1st case is
470                          *      common than the 2nd case, trigger OI scrub. */
471                         result = osd_oi_lookup(info, dev, fid, id, true);
472                         if (result == 0)
473                                 /* It is the case 1 or 2. */
474                                 goto trigger;
475                 } else if (result == -EREMCHG) {
476
477 trigger:
478                         if (unlikely(triggered))
479                                 GOTO(out, result = saved);
480
481                         triggered = true;
482                         if (thread_is_running(&scrub->os_thread)) {
483                                 result = -EINPROGRESS;
484                         } else if (!dev->od_noscrub) {
485                                 result = osd_scrub_start(dev);
486                                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
487                                                "for "DFID", rc = %d [1]\n",
488                                                LDISKFS_SB(osd_sb(dev))->s_es->\
489                                                s_volume_name,PFID(fid), result);
490                                 if (result == 0 || result == -EALREADY)
491                                         result = -EINPROGRESS;
492                                 else
493                                         result = -EREMCHG;
494                         }
495
496                         /* We still have chance to get the valid inode: for the
497                          * object which is referenced by remote name entry, the
498                          * object on the local MDT will be linked under the dir
499                          * of "/REMOTE_PARENT_DIR" with its FID string as name.
500                          *
501                          * We do not know whether the object for the given FID
502                          * is referenced by some remote name entry or not, and
503                          * especially for DNE II, a multiple-linked object may
504                          * have many name entries reside on many MDTs.
505                          *
506                          * To simplify the operation, OSD will not distinguish
507                          * more, just lookup "/REMOTE_PARENT_DIR". Usually, it
508                          * only happened for the RPC from other MDT during the
509                          * OI scrub, or for the client side RPC with FID only,
510                          * such as FID to path, or from old connected client. */
511                         saved = result;
512                         result = osd_lookup_in_remote_parent(info, dev,
513                                                              fid, id);
514                         if (result == 0) {
515                                 in_oi = false;
516                                 goto iget;
517                         }
518
519                         result = saved;
520                 }
521
522                 GOTO(out, result);
523         }
524
525         obj->oo_inode = inode;
526         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
527
528         result = osd_check_lma(env, obj);
529         if (result != 0) {
530                 iput(inode);
531                 obj->oo_inode = NULL;
532                 if (result == -EREMCHG)
533                         goto trigger;
534
535                 GOTO(out, result);
536         }
537
538         obj->oo_compat_dot_created = 1;
539         obj->oo_compat_dotdot_created = 1;
540
541         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
542                 GOTO(out, result = 0);
543
544         LASSERT(obj->oo_hl_head == NULL);
545         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
546         if (obj->oo_hl_head == NULL) {
547                 obj->oo_inode = NULL;
548                 iput(inode);
549                 GOTO(out, result = -ENOMEM);
550         }
551         GOTO(out, result = 0);
552
553 out:
554         LINVRNT(osd_invariant(obj));
555         return result;
556 }
557
558 /*
559  * Concurrency: shouldn't matter.
560  */
561 static void osd_object_init0(struct osd_object *obj)
562 {
563         LASSERT(obj->oo_inode != NULL);
564         obj->oo_dt.do_body_ops = &osd_body_ops;
565         obj->oo_dt.do_lu.lo_header->loh_attr |=
566                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
567 }
568
569 /*
570  * Concurrency: no concurrent access is possible that early in object
571  * life-cycle.
572  */
573 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
574                            const struct lu_object_conf *conf)
575 {
576         struct osd_object *obj = osd_obj(l);
577         int result;
578
579         LINVRNT(osd_invariant(obj));
580
581         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
582                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
583                 l->lo_header->loh_attr |= LOHA_EXISTS;
584                 return 0;
585         }
586
587         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
588         obj->oo_dt.do_body_ops = &osd_body_ops_new;
589         if (result == 0 && obj->oo_inode != NULL)
590                 osd_object_init0(obj);
591
592         LINVRNT(osd_invariant(obj));
593         return result;
594 }
595
596 /*
597  * Concurrency: no concurrent access is possible that late in object
598  * life-cycle.
599  */
600 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
601 {
602         struct osd_object *obj = osd_obj(l);
603
604         LINVRNT(osd_invariant(obj));
605
606         dt_object_fini(&obj->oo_dt);
607         if (obj->oo_hl_head != NULL)
608                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
609         OBD_FREE_PTR(obj);
610 }
611
612 /*
613  * Concurrency: no concurrent access is possible that late in object
614  * life-cycle.
615  */
616 static void osd_index_fini(struct osd_object *o)
617 {
618         struct iam_container *bag;
619
620         if (o->oo_dir != NULL) {
621                 bag = &o->oo_dir->od_container;
622                 if (o->oo_inode != NULL) {
623                         if (bag->ic_object == o->oo_inode)
624                                 iam_container_fini(bag);
625                 }
626                 OBD_FREE_PTR(o->oo_dir);
627                 o->oo_dir = NULL;
628         }
629 }
630
631 /*
632  * Concurrency: no concurrent access is possible that late in object
633  * life-cycle (for all existing callers, that is. New callers have to provide
634  * their own locking.)
635  */
636 static int osd_inode_unlinked(const struct inode *inode)
637 {
638         return inode->i_nlink == 0;
639 }
640
641 enum {
642         OSD_TXN_OI_DELETE_CREDITS    = 20,
643         OSD_TXN_INODE_DELETE_CREDITS = 20
644 };
645
646 /*
647  * Journal
648  */
649
650 #if OSD_THANDLE_STATS
651 /**
652  * Set time when the handle is allocated
653  */
654 static void osd_th_alloced(struct osd_thandle *oth)
655 {
656         oth->oth_alloced = cfs_time_current();
657 }
658
659 /**
660  * Set time when the handle started
661  */
662 static void osd_th_started(struct osd_thandle *oth)
663 {
664         oth->oth_started = cfs_time_current();
665 }
666
667 /**
668  * Helper function to convert time interval to microseconds packed in
669  * long int.
670  */
671 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
672 {
673         struct timeval val;
674
675         cfs_duration_usec(cfs_time_sub(end, start), &val);
676         return val.tv_sec * 1000000 + val.tv_usec;
677 }
678
679 /**
680  * Check whether the we deal with this handle for too long.
681  */
682 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
683                                 cfs_time_t alloced, cfs_time_t started,
684                                 cfs_time_t closed)
685 {
686         cfs_time_t now = cfs_time_current();
687
688         LASSERT(dev != NULL);
689
690         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
691                             interval_to_usec(alloced, started));
692         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
693                             interval_to_usec(started, closed));
694         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
695                             interval_to_usec(closed, now));
696
697         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
698                 CWARN("transaction handle %p was open for too long: "
699                       "now "CFS_TIME_T" ,"
700                       "alloced "CFS_TIME_T" ,"
701                       "started "CFS_TIME_T" ,"
702                       "closed "CFS_TIME_T"\n",
703                       oth, now, alloced, started, closed);
704                 libcfs_debug_dumpstack(NULL);
705         }
706 }
707
708 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
709 {                                                                       \
710         cfs_time_t __closed = cfs_time_current();                       \
711         cfs_time_t __alloced = oth->oth_alloced;                        \
712         cfs_time_t __started = oth->oth_started;                        \
713                                                                         \
714         expr;                                                           \
715         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
716 }
717
718 #else /* OSD_THANDLE_STATS */
719
720 #define osd_th_alloced(h)                  do {} while(0)
721 #define osd_th_started(h)                  do {} while(0)
722 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
723
724 #endif /* OSD_THANDLE_STATS */
725
726 /*
727  * Concurrency: doesn't access mutable data.
728  */
729 static int osd_param_is_not_sane(const struct osd_device *dev,
730                                  const struct thandle *th)
731 {
732         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
733
734         return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
735 }
736
737 /*
738  * Concurrency: shouldn't matter.
739  */
740 static void osd_trans_commit_cb(struct super_block *sb,
741                                 struct ldiskfs_journal_cb_entry *jcb, int error)
742 {
743         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
744         struct thandle     *th  = &oh->ot_super;
745         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
746         struct dt_txn_commit_cb *dcb, *tmp;
747
748         LASSERT(oh->ot_handle == NULL);
749
750         if (error)
751                 CERROR("transaction @0x%p commit error: %d\n", th, error);
752
753         dt_txn_hook_commit(th);
754
755         /* call per-transaction callbacks if any */
756         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
757                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
758                          "commit callback entry: magic=%x name='%s'\n",
759                          dcb->dcb_magic, dcb->dcb_name);
760                 cfs_list_del_init(&dcb->dcb_linkage);
761                 dcb->dcb_func(NULL, th, dcb, error);
762         }
763
764         lu_ref_del_at(&lud->ld_reference, &oh->ot_dev_link, "osd-tx", th);
765         lu_device_put(lud);
766         th->th_dev = NULL;
767
768         lu_context_exit(&th->th_ctx);
769         lu_context_fini(&th->th_ctx);
770         OBD_FREE_PTR(oh);
771 }
772
773 static struct thandle *osd_trans_create(const struct lu_env *env,
774                                         struct dt_device *d)
775 {
776         struct osd_thread_info *oti = osd_oti_get(env);
777         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
778         struct osd_thandle     *oh;
779         struct thandle         *th;
780         ENTRY;
781
782         /* on pending IO in this thread should left from prev. request */
783         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
784
785         th = ERR_PTR(-ENOMEM);
786         OBD_ALLOC_GFP(oh, sizeof *oh, __GFP_IO);
787         if (oh != NULL) {
788                 oh->ot_quota_trans = &oti->oti_quota_trans;
789                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
790                 th = &oh->ot_super;
791                 th->th_dev = d;
792                 th->th_result = 0;
793                 th->th_tags = LCT_TX_HANDLE;
794                 oh->ot_credits = 0;
795                 oti->oti_dev = osd_dt_dev(d);
796                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
797                 osd_th_alloced(oh);
798
799                 memset(oti->oti_declare_ops, 0,
800                                         sizeof(oti->oti_declare_ops));
801                 memset(oti->oti_declare_ops_rb, 0,
802                                         sizeof(oti->oti_declare_ops_rb));
803                 memset(oti->oti_declare_ops_cred, 0,
804                                         sizeof(oti->oti_declare_ops_cred));
805                 oti->oti_rollback = false;
806         }
807         RETURN(th);
808 }
809
810 /*
811  * Concurrency: shouldn't matter.
812  */
813 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
814                     struct thandle *th)
815 {
816         struct osd_thread_info *oti = osd_oti_get(env);
817         struct osd_device  *dev = osd_dt_dev(d);
818         handle_t           *jh;
819         struct osd_thandle *oh;
820         int rc;
821
822         ENTRY;
823
824         LASSERT(current->journal_info == NULL);
825
826         oh = container_of0(th, struct osd_thandle, ot_super);
827         LASSERT(oh != NULL);
828         LASSERT(oh->ot_handle == NULL);
829
830         rc = dt_txn_hook_start(env, d, th);
831         if (rc != 0)
832                 GOTO(out, rc);
833
834         if (unlikely(osd_param_is_not_sane(dev, th))) {
835                 static unsigned long last_printed;
836                 static int last_credits;
837
838                 CWARN("%.16s: too many transaction credits (%d > %d)\n",
839                       LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
840                       oh->ot_credits,
841                       osd_journal(dev)->j_max_transaction_buffers);
842                 CWARN("  create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
843                       oti->oti_declare_ops[OSD_OT_CREATE],
844                       oti->oti_declare_ops_cred[OSD_OT_CREATE],
845                       oti->oti_declare_ops[OSD_OT_DELETE],
846                       oti->oti_declare_ops_cred[OSD_OT_DELETE],
847                       oti->oti_declare_ops[OSD_OT_DESTROY],
848                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
849                 CWARN("  attr_set: %u/%u, xattr_set: %u/%u\n",
850                       oti->oti_declare_ops[OSD_OT_ATTR_SET],
851                       oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
852                       oti->oti_declare_ops[OSD_OT_XATTR_SET],
853                       oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
854                 CWARN("  write: %u/%u, punch: %u/%u, quota %u/%u\n",
855                       oti->oti_declare_ops[OSD_OT_WRITE],
856                       oti->oti_declare_ops_cred[OSD_OT_WRITE],
857                       oti->oti_declare_ops[OSD_OT_PUNCH],
858                       oti->oti_declare_ops_cred[OSD_OT_PUNCH],
859                       oti->oti_declare_ops[OSD_OT_QUOTA],
860                       oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
861                 CWARN("  insert: %u/%u, delete: %u/%u\n",
862                       oti->oti_declare_ops[OSD_OT_INSERT],
863                       oti->oti_declare_ops_cred[OSD_OT_INSERT],
864                       oti->oti_declare_ops[OSD_OT_DESTROY],
865                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
866                 CWARN("  ref_add: %u/%u, ref_del: %u/%u\n",
867                       oti->oti_declare_ops[OSD_OT_REF_ADD],
868                       oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
869                       oti->oti_declare_ops[OSD_OT_REF_DEL],
870                       oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
871
872                 if (last_credits != oh->ot_credits &&
873                     time_after(jiffies, last_printed + 60 * HZ)) {
874                         libcfs_debug_dumpstack(NULL);
875                         last_credits = oh->ot_credits;
876                         last_printed = jiffies;
877                 }
878                 /* XXX Limit the credits to 'max_transaction_buffers', and
879                  *     let the underlying filesystem to catch the error if
880                  *     we really need so many credits.
881                  *
882                  *     This should be removed when we can calculate the
883                  *     credits precisely. */
884                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
885         }
886
887         /*
888          * XXX temporary stuff. Some abstraction layer should
889          * be used.
890          */
891         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
892         osd_th_started(oh);
893         if (!IS_ERR(jh)) {
894                 oh->ot_handle = jh;
895                 LASSERT(oti->oti_txns == 0);
896                 lu_context_init(&th->th_ctx, th->th_tags);
897                 lu_context_enter(&th->th_ctx);
898
899                 lu_device_get(&d->dd_lu_dev);
900                 lu_ref_add_at(&d->dd_lu_dev.ld_reference, &oh->ot_dev_link,
901                               "osd-tx", th);
902                 oti->oti_txns++;
903                 rc = 0;
904         } else {
905                 rc = PTR_ERR(jh);
906         }
907 out:
908         RETURN(rc);
909 }
910
911 /*
912  * Concurrency: shouldn't matter.
913  */
914 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
915 {
916         int                     rc = 0;
917         struct osd_thandle     *oh;
918         struct osd_thread_info *oti = osd_oti_get(env);
919         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
920         struct qsd_instance    *qsd = oti->oti_dev->od_quota_slave;
921         ENTRY;
922
923         oh = container_of0(th, struct osd_thandle, ot_super);
924
925         if (qsd != NULL)
926                 /* inform the quota slave device that the transaction is
927                  * stopping */
928                 qsd_op_end(env, qsd, oh->ot_quota_trans);
929         oh->ot_quota_trans = NULL;
930
931         if (oh->ot_handle != NULL) {
932                 handle_t *hdl = oh->ot_handle;
933
934                 /*
935                  * add commit callback
936                  * notice we don't do this in osd_trans_start()
937                  * as underlying transaction can change during truncate
938                  */
939                 ldiskfs_journal_callback_add(hdl, osd_trans_commit_cb,
940                                          &oh->ot_jcb);
941
942                 LASSERT(oti->oti_txns == 1);
943                 oti->oti_txns--;
944                 rc = dt_txn_hook_stop(env, th);
945                 if (rc != 0)
946                         CERROR("Failure in transaction hook: %d\n", rc);
947
948                 /* hook functions might modify th_sync */
949                 hdl->h_sync = th->th_sync;
950
951                 oh->ot_handle = NULL;
952                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
953                                   rc = ldiskfs_journal_stop(hdl));
954                 if (rc != 0)
955                         CERROR("Failure to stop transaction: %d\n", rc);
956         } else {
957                 OBD_FREE_PTR(oh);
958         }
959
960         /* as we want IO to journal and data IO be concurrent, we don't block
961          * awaiting data IO completion in osd_do_bio(), instead we wait here
962          * once transaction is submitted to the journal. all reqular requests
963          * don't do direct IO (except read/write), thus this wait_event becomes
964          * no-op for them.
965          *
966          * IMPORTANT: we have to wait till any IO submited by the thread is
967          * completed otherwise iobuf may be corrupted by different request
968          */
969         wait_event(iobuf->dr_wait,
970                        cfs_atomic_read(&iobuf->dr_numreqs) == 0);
971         if (!rc)
972                 rc = iobuf->dr_error;
973
974         RETURN(rc);
975 }
976
977 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
978 {
979         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
980                                                ot_super);
981
982         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
983         LASSERT(&dcb->dcb_func != NULL);
984         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
985
986         return 0;
987 }
988
989 /*
990  * Called just before object is freed. Releases all resources except for
991  * object itself (that is released by osd_object_free()).
992  *
993  * Concurrency: no concurrent access is possible that late in object
994  * life-cycle.
995  */
996 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
997 {
998         struct osd_object *obj   = osd_obj(l);
999         struct inode      *inode = obj->oo_inode;
1000
1001         LINVRNT(osd_invariant(obj));
1002
1003         /*
1004          * If object is unlinked remove fid->ino mapping from object index.
1005          */
1006
1007         osd_index_fini(obj);
1008         if (inode != NULL) {
1009                 struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
1010                 qid_t                    uid = inode->i_uid;
1011                 qid_t                    gid = inode->i_gid;
1012
1013                 iput(inode);
1014                 obj->oo_inode = NULL;
1015
1016                 if (qsd != NULL) {
1017                         struct osd_thread_info  *info = osd_oti_get(env);
1018                         struct lquota_id_info   *qi = &info->oti_qi;
1019
1020                         /* Release granted quota to master if necessary */
1021                         qi->lqi_id.qid_uid = uid;
1022                         qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
1023
1024                         qi->lqi_id.qid_uid = gid;
1025                         qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
1026                 }
1027         }
1028 }
1029
1030 /*
1031  * Concurrency: ->loo_object_release() is called under site spin-lock.
1032  */
1033 static void osd_object_release(const struct lu_env *env,
1034                                struct lu_object *l)
1035 {
1036 }
1037
1038 /*
1039  * Concurrency: shouldn't matter.
1040  */
1041 static int osd_object_print(const struct lu_env *env, void *cookie,
1042                             lu_printer_t p, const struct lu_object *l)
1043 {
1044         struct osd_object *o = osd_obj(l);
1045         struct iam_descr  *d;
1046
1047         if (o->oo_dir != NULL)
1048                 d = o->oo_dir->od_container.ic_descr;
1049         else
1050                 d = NULL;
1051         return (*p)(env, cookie,
1052                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1053                     o, o->oo_inode,
1054                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
1055                     o->oo_inode ? o->oo_inode->i_generation : 0,
1056                     d ? d->id_ops->id_name : "plain");
1057 }
1058
1059 /*
1060  * Concurrency: shouldn't matter.
1061  */
1062 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1063                struct obd_statfs *sfs)
1064 {
1065         struct osd_device  *osd = osd_dt_dev(d);
1066         struct super_block *sb = osd_sb(osd);
1067         struct kstatfs     *ksfs;
1068         int result = 0;
1069
1070         if (unlikely(osd->od_mnt == NULL))
1071                 return -EINPROGRESS;
1072
1073         /* osd_lproc.c call this without env, allocate ksfs for that case */
1074         if (unlikely(env == NULL)) {
1075                 OBD_ALLOC_PTR(ksfs);
1076                 if (ksfs == NULL)
1077                         return -ENOMEM;
1078         } else {
1079                 ksfs = &osd_oti_get(env)->oti_ksfs;
1080         }
1081
1082         spin_lock(&osd->od_osfs_lock);
1083         /* cache 1 second */
1084         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1085                 result = sb->s_op->statfs(sb->s_root, ksfs);
1086                 if (likely(result == 0)) { /* N.B. statfs can't really fail */
1087                         osd->od_osfs_age = cfs_time_current_64();
1088                         statfs_pack(&osd->od_statfs, ksfs);
1089                         if (sb->s_flags & MS_RDONLY)
1090                                 sfs->os_state = OS_STATE_READONLY;
1091                 }
1092         }
1093
1094         if (likely(result == 0))
1095                 *sfs = osd->od_statfs;
1096         spin_unlock(&osd->od_osfs_lock);
1097
1098         if (unlikely(env == NULL))
1099                 OBD_FREE_PTR(ksfs);
1100
1101         return result;
1102 }
1103
1104 /**
1105  * Estimate space needed for file creations. We assume the largest filename
1106  * which is 2^64 - 1, hence a filename of 20 chars.
1107  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
1108  */
1109 #ifdef __LDISKFS_DIR_REC_LEN
1110 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1111 #else
1112 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1113 #endif
1114
1115 /*
1116  * Concurrency: doesn't access mutable data.
1117  */
1118 static void osd_conf_get(const struct lu_env *env,
1119                          const struct dt_device *dev,
1120                          struct dt_device_param *param)
1121 {
1122         struct super_block *sb = osd_sb(osd_dt_dev(dev));
1123
1124         /*
1125          * XXX should be taken from not-yet-existing fs abstraction layer.
1126          */
1127         param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1128         param->ddp_max_name_len = LDISKFS_NAME_LEN;
1129         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1130         param->ddp_block_shift  = sb->s_blocksize_bits;
1131         param->ddp_mount_type     = LDD_MT_LDISKFS;
1132         param->ddp_maxbytes       = sb->s_maxbytes;
1133         /* Overhead estimate should be fairly accurate, so we really take a tiny
1134          * error margin which also avoids fragmenting the filesystem too much */
1135         param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1136         /* inode are statically allocated, so per-inode space consumption
1137          * is the space consumed by the directory entry */
1138         param->ddp_inodespace     = PER_OBJ_USAGE;
1139         /* per-fragment overhead to be used by the client code */
1140         param->ddp_grant_frag     = 6 * LDISKFS_BLOCK_SIZE(sb);
1141         param->ddp_mntopts      = 0;
1142         if (test_opt(sb, XATTR_USER))
1143                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1144         if (test_opt(sb, POSIX_ACL))
1145                 param->ddp_mntopts |= MNTOPT_ACL;
1146
1147 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1148         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1149                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1150         else
1151 #endif
1152                 param->ddp_max_ea_size = sb->s_blocksize;
1153
1154 }
1155
1156 /*
1157  * Concurrency: shouldn't matter.
1158  */
1159 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1160 {
1161         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1162         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1163 }
1164
1165 /**
1166  * Start commit for OSD device.
1167  *
1168  * An implementation of dt_commit_async method for OSD device.
1169  * Asychronously starts underlayng fs sync and thereby a transaction
1170  * commit.
1171  *
1172  * \param env environment
1173  * \param d dt device
1174  *
1175  * \see dt_device_operations
1176  */
1177 static int osd_commit_async(const struct lu_env *env,
1178                             struct dt_device *d)
1179 {
1180         struct super_block *s = osd_sb(osd_dt_dev(d));
1181         ENTRY;
1182
1183         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1184         RETURN(s->s_op->sync_fs(s, 0));
1185 }
1186
1187 /*
1188  * Concurrency: shouldn't matter.
1189  */
1190
1191 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1192 {
1193         struct super_block *sb = osd_sb(osd_dt_dev(d));
1194         struct block_device *dev = sb->s_bdev;
1195 #ifdef HAVE_DEV_SET_RDONLY
1196         struct block_device *jdev = LDISKFS_SB(sb)->journal_bdev;
1197         int rc = 0;
1198 #else
1199         int rc = -EOPNOTSUPP;
1200 #endif
1201         ENTRY;
1202
1203 #ifdef HAVE_DEV_SET_RDONLY
1204         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1205
1206         if (jdev && (jdev != dev)) {
1207                 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
1208                        (long)jdev);
1209                 dev_set_rdonly(jdev);
1210         }
1211         CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
1212         dev_set_rdonly(dev);
1213 #else
1214         CERROR("%s: %lx CANNOT BE SET READONLY: rc = %d\n",
1215                osd_dt_dev(d)->od_svname, (long)dev, rc);
1216 #endif
1217         RETURN(rc);
1218 }
1219
1220 /*
1221  * Concurrency: serialization provided by callers.
1222  */
1223 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1224                               int mode, unsigned long timeout, __u32 alg,
1225                               struct lustre_capa_key *keys)
1226 {
1227         struct osd_device *dev = osd_dt_dev(d);
1228         ENTRY;
1229
1230         dev->od_fl_capa = mode;
1231         dev->od_capa_timeout = timeout;
1232         dev->od_capa_alg = alg;
1233         dev->od_capa_keys = keys;
1234         RETURN(0);
1235 }
1236
1237 /**
1238  * Note: we do not count into QUOTA here.
1239  * If we mount with --data_journal we may need more.
1240  */
1241 const int osd_dto_credits_noquota[DTO_NR] = {
1242         /**
1243          * Insert/Delete.
1244          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1245          * SINGLEDATA_TRANS_BLOCKS(8)
1246          * XXX Note: maybe iam need more, since iam have more level than
1247          *           EXT3 htree.
1248          */
1249         [DTO_INDEX_INSERT]  = 16,
1250         [DTO_INDEX_DELETE]  = 16,
1251         /**
1252          * Used for OI scrub
1253          */
1254         [DTO_INDEX_UPDATE]  = 16,
1255         /**
1256          * Create a object. The same as create object in EXT3.
1257          * DATA_TRANS_BLOCKS(14) +
1258          * INDEX_EXTRA_BLOCKS(8) +
1259          * 3(inode bits, groups, GDT)
1260          */
1261         [DTO_OBJECT_CREATE] = 25,
1262         /**
1263          * XXX: real credits to be fixed
1264          */
1265         [DTO_OBJECT_DELETE] = 25,
1266         /**
1267          * Attr set credits (inode)
1268          */
1269         [DTO_ATTR_SET_BASE] = 1,
1270         /**
1271          * Xattr set. The same as xattr of EXT3.
1272          * DATA_TRANS_BLOCKS(14)
1273          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1274          * are also counted in. Do not know why?
1275          */
1276         [DTO_XATTR_SET]     = 14,
1277         [DTO_LOG_REC]       = 14,
1278         /**
1279          * credits for inode change during write.
1280          */
1281         [DTO_WRITE_BASE]    = 3,
1282         /**
1283          * credits for single block write.
1284          */
1285         [DTO_WRITE_BLOCK]   = 14,
1286         /**
1287          * Attr set credits for chown.
1288          * This is extra credits for setattr, and it is null without quota
1289          */
1290         [DTO_ATTR_SET_CHOWN]= 0
1291 };
1292
1293 static const struct dt_device_operations osd_dt_ops = {
1294         .dt_root_get       = osd_root_get,
1295         .dt_statfs         = osd_statfs,
1296         .dt_trans_create   = osd_trans_create,
1297         .dt_trans_start    = osd_trans_start,
1298         .dt_trans_stop     = osd_trans_stop,
1299         .dt_trans_cb_add   = osd_trans_cb_add,
1300         .dt_conf_get       = osd_conf_get,
1301         .dt_sync           = osd_sync,
1302         .dt_ro             = osd_ro,
1303         .dt_commit_async   = osd_commit_async,
1304         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1305 };
1306
1307 static void osd_object_read_lock(const struct lu_env *env,
1308                                  struct dt_object *dt, unsigned role)
1309 {
1310         struct osd_object *obj = osd_dt_obj(dt);
1311         struct osd_thread_info *oti = osd_oti_get(env);
1312
1313         LINVRNT(osd_invariant(obj));
1314
1315         LASSERT(obj->oo_owner != env);
1316         down_read_nested(&obj->oo_sem, role);
1317
1318         LASSERT(obj->oo_owner == NULL);
1319         oti->oti_r_locks++;
1320 }
1321
1322 static void osd_object_write_lock(const struct lu_env *env,
1323                                   struct dt_object *dt, unsigned role)
1324 {
1325         struct osd_object *obj = osd_dt_obj(dt);
1326         struct osd_thread_info *oti = osd_oti_get(env);
1327
1328         LINVRNT(osd_invariant(obj));
1329
1330         LASSERT(obj->oo_owner != env);
1331         down_write_nested(&obj->oo_sem, role);
1332
1333         LASSERT(obj->oo_owner == NULL);
1334         obj->oo_owner = env;
1335         oti->oti_w_locks++;
1336 }
1337
1338 static void osd_object_read_unlock(const struct lu_env *env,
1339                                    struct dt_object *dt)
1340 {
1341         struct osd_object *obj = osd_dt_obj(dt);
1342         struct osd_thread_info *oti = osd_oti_get(env);
1343
1344         LINVRNT(osd_invariant(obj));
1345
1346         LASSERT(oti->oti_r_locks > 0);
1347         oti->oti_r_locks--;
1348         up_read(&obj->oo_sem);
1349 }
1350
1351 static void osd_object_write_unlock(const struct lu_env *env,
1352                                     struct dt_object *dt)
1353 {
1354         struct osd_object *obj = osd_dt_obj(dt);
1355         struct osd_thread_info *oti = osd_oti_get(env);
1356
1357         LINVRNT(osd_invariant(obj));
1358
1359         LASSERT(obj->oo_owner == env);
1360         LASSERT(oti->oti_w_locks > 0);
1361         oti->oti_w_locks--;
1362         obj->oo_owner = NULL;
1363         up_write(&obj->oo_sem);
1364 }
1365
1366 static int osd_object_write_locked(const struct lu_env *env,
1367                                    struct dt_object *dt)
1368 {
1369         struct osd_object *obj = osd_dt_obj(dt);
1370
1371         LINVRNT(osd_invariant(obj));
1372
1373         return obj->oo_owner == env;
1374 }
1375
1376 static int capa_is_sane(const struct lu_env *env,
1377                         struct osd_device *dev,
1378                         struct lustre_capa *capa,
1379                         struct lustre_capa_key *keys)
1380 {
1381         struct osd_thread_info *oti = osd_oti_get(env);
1382         struct lustre_capa *tcapa = &oti->oti_capa;
1383         struct obd_capa *oc;
1384         int i, rc = 0;
1385         ENTRY;
1386
1387         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1388         if (oc) {
1389                 if (capa_is_expired(oc)) {
1390                         DEBUG_CAPA(D_ERROR, capa, "expired");
1391                         rc = -ESTALE;
1392                 }
1393                 capa_put(oc);
1394                 RETURN(rc);
1395         }
1396
1397         if (capa_is_expired_sec(capa)) {
1398                 DEBUG_CAPA(D_ERROR, capa, "expired");
1399                 RETURN(-ESTALE);
1400         }
1401
1402         spin_lock(&capa_lock);
1403         for (i = 0; i < 2; i++) {
1404                 if (keys[i].lk_keyid == capa->lc_keyid) {
1405                         oti->oti_capa_key = keys[i];
1406                         break;
1407                 }
1408         }
1409         spin_unlock(&capa_lock);
1410
1411         if (i == 2) {
1412                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1413                 RETURN(-ESTALE);
1414         }
1415
1416         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1417         if (rc)
1418                 RETURN(rc);
1419
1420         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1421                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1422                 RETURN(-EACCES);
1423         }
1424
1425         oc = capa_add(dev->od_capa_hash, capa);
1426         capa_put(oc);
1427
1428         RETURN(0);
1429 }
1430
1431 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1432                     struct lustre_capa *capa, __u64 opc)
1433 {
1434         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1435         struct osd_device *osd = osd_dev(dt->do_lu.lo_dev);
1436         struct lu_capainfo *lci;
1437         int rc;
1438
1439         if (!osd->od_fl_capa)
1440                 return 0;
1441
1442         if (capa == BYPASS_CAPA)
1443                 return 0;
1444
1445         lci = lu_capainfo_get(env);
1446         if (unlikely(lci == NULL))
1447                 return 0;
1448
1449         if (lci->lci_auth == LC_ID_NONE)
1450                 return 0;
1451
1452         if (capa == NULL) {
1453                 CERROR("%s: no capability provided for FID "DFID": rc = %d\n",
1454                        osd_name(osd), PFID(fid), -EACCES);
1455                 return -EACCES;
1456         }
1457
1458         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1459                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1460                            PFID(fid));
1461                 return -EACCES;
1462         }
1463
1464         if (!capa_opc_supported(capa, opc)) {
1465                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1466                 return -EACCES;
1467         }
1468
1469         rc = capa_is_sane(env, osd, capa, osd->od_capa_keys);
1470         if (rc != 0) {
1471                 DEBUG_CAPA(D_ERROR, capa, "insane: rc = %d", rc);
1472                 return -EACCES;
1473         }
1474
1475         return 0;
1476 }
1477
1478 static struct timespec *osd_inode_time(const struct lu_env *env,
1479                                        struct inode *inode, __u64 seconds)
1480 {
1481         struct osd_thread_info  *oti = osd_oti_get(env);
1482         struct timespec         *t   = &oti->oti_time;
1483
1484         t->tv_sec = seconds;
1485         t->tv_nsec = 0;
1486         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1487         return t;
1488 }
1489
1490
1491 static void osd_inode_getattr(const struct lu_env *env,
1492                               struct inode *inode, struct lu_attr *attr)
1493 {
1494         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1495                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1496                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE |
1497                                LA_TYPE;
1498
1499         attr->la_atime      = LTIME_S(inode->i_atime);
1500         attr->la_mtime      = LTIME_S(inode->i_mtime);
1501         attr->la_ctime      = LTIME_S(inode->i_ctime);
1502         attr->la_mode       = inode->i_mode;
1503         attr->la_size       = i_size_read(inode);
1504         attr->la_blocks     = inode->i_blocks;
1505         attr->la_uid        = inode->i_uid;
1506         attr->la_gid        = inode->i_gid;
1507         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1508         attr->la_nlink      = inode->i_nlink;
1509         attr->la_rdev       = inode->i_rdev;
1510         attr->la_blksize    = 1 << inode->i_blkbits;
1511         attr->la_blkbits    = inode->i_blkbits;
1512 }
1513
1514 static int osd_attr_get(const struct lu_env *env,
1515                         struct dt_object *dt,
1516                         struct lu_attr *attr,
1517                         struct lustre_capa *capa)
1518 {
1519         struct osd_object *obj = osd_dt_obj(dt);
1520
1521         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1522         LINVRNT(osd_invariant(obj));
1523
1524         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1525                 return -EACCES;
1526
1527         spin_lock(&obj->oo_guard);
1528         osd_inode_getattr(env, obj->oo_inode, attr);
1529         spin_unlock(&obj->oo_guard);
1530         return 0;
1531 }
1532
1533 static int osd_declare_attr_set(const struct lu_env *env,
1534                                 struct dt_object *dt,
1535                                 const struct lu_attr *attr,
1536                                 struct thandle *handle)
1537 {
1538         struct osd_thandle     *oh;
1539         struct osd_object      *obj;
1540         struct osd_thread_info *info = osd_oti_get(env);
1541         struct lquota_id_info  *qi = &info->oti_qi;
1542         long long               bspace;
1543         int                     rc = 0;
1544         bool                    allocated;
1545         ENTRY;
1546
1547         LASSERT(dt != NULL);
1548         LASSERT(handle != NULL);
1549
1550         obj = osd_dt_obj(dt);
1551         LASSERT(osd_invariant(obj));
1552
1553         oh = container_of0(handle, struct osd_thandle, ot_super);
1554         LASSERT(oh->ot_handle == NULL);
1555
1556         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1557                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1558
1559         if (attr == NULL || obj->oo_inode == NULL)
1560                 RETURN(rc);
1561
1562         bspace   = obj->oo_inode->i_blocks;
1563         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1564         bspace   = toqb(bspace);
1565
1566         /* Changing ownership is always preformed by super user, it should not
1567          * fail with EDQUOT.
1568          *
1569          * We still need to call the osd_declare_qid() to calculate the journal
1570          * credits for updating quota accounting files and to trigger quota
1571          * space adjustment once the operation is completed.*/
1572         if ((attr->la_valid & LA_UID) != 0 &&
1573              attr->la_uid != obj->oo_inode->i_uid) {
1574                 qi->lqi_type = USRQUOTA;
1575
1576                 /* inode accounting */
1577                 qi->lqi_is_blk = false;
1578
1579                 /* one more inode for the new owner ... */
1580                 qi->lqi_id.qid_uid = attr->la_uid;
1581                 qi->lqi_space      = 1;
1582                 allocated = (attr->la_uid == 0) ? true : false;
1583                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1584                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1585                         rc = 0;
1586                 if (rc)
1587                         RETURN(rc);
1588
1589                 /* and one less inode for the current uid */
1590                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1591                 qi->lqi_space      = -1;
1592                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1593                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1594                         rc = 0;
1595                 if (rc)
1596                         RETURN(rc);
1597
1598                 /* block accounting */
1599                 qi->lqi_is_blk = true;
1600
1601                 /* more blocks for the new owner ... */
1602                 qi->lqi_id.qid_uid = attr->la_uid;
1603                 qi->lqi_space      = bspace;
1604                 allocated = (attr->la_uid == 0) ? true : false;
1605                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1606                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1607                         rc = 0;
1608                 if (rc)
1609                         RETURN(rc);
1610
1611                 /* and finally less blocks for the current owner */
1612                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1613                 qi->lqi_space      = -bspace;
1614                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1615                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1616                         rc = 0;
1617                 if (rc)
1618                         RETURN(rc);
1619         }
1620
1621         if (attr->la_valid & LA_GID &&
1622             attr->la_gid != obj->oo_inode->i_gid) {
1623                 qi->lqi_type = GRPQUOTA;
1624
1625                 /* inode accounting */
1626                 qi->lqi_is_blk = false;
1627
1628                 /* one more inode for the new group owner ... */
1629                 qi->lqi_id.qid_gid = attr->la_gid;
1630                 qi->lqi_space      = 1;
1631                 allocated = (attr->la_gid == 0) ? true : false;
1632                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1633                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1634                         rc = 0;
1635                 if (rc)
1636                         RETURN(rc);
1637
1638                 /* and one less inode for the current gid */
1639                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1640                 qi->lqi_space      = -1;
1641                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1642                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1643                         rc = 0;
1644                 if (rc)
1645                         RETURN(rc);
1646
1647                 /* block accounting */
1648                 qi->lqi_is_blk = true;
1649
1650                 /* more blocks for the new owner ... */
1651                 qi->lqi_id.qid_gid = attr->la_gid;
1652                 qi->lqi_space      = bspace;
1653                 allocated = (attr->la_gid == 0) ? true : false;
1654                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1655                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1656                         rc = 0;
1657                 if (rc)
1658                         RETURN(rc);
1659
1660                 /* and finally less blocks for the current owner */
1661                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1662                 qi->lqi_space      = -bspace;
1663                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1664                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1665                         rc = 0;
1666                 if (rc)
1667                         RETURN(rc);
1668         }
1669
1670         RETURN(rc);
1671 }
1672
1673 static int osd_inode_setattr(const struct lu_env *env,
1674                              struct inode *inode, const struct lu_attr *attr)
1675 {
1676         __u64 bits;
1677
1678         bits = attr->la_valid;
1679
1680         if (bits & LA_ATIME)
1681                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1682         if (bits & LA_CTIME)
1683                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1684         if (bits & LA_MTIME)
1685                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1686         if (bits & LA_SIZE) {
1687                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1688                 i_size_write(inode, attr->la_size);
1689         }
1690
1691 #if 0
1692         /* OSD should not change "i_blocks" which is used by quota.
1693          * "i_blocks" should be changed by ldiskfs only. */
1694         if (bits & LA_BLOCKS)
1695                 inode->i_blocks = attr->la_blocks;
1696 #endif
1697         if (bits & LA_MODE)
1698                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1699                         (attr->la_mode & ~S_IFMT);
1700         if (bits & LA_UID)
1701                 inode->i_uid    = attr->la_uid;
1702         if (bits & LA_GID)
1703                 inode->i_gid    = attr->la_gid;
1704         if (bits & LA_NLINK)
1705                 set_nlink(inode, attr->la_nlink);
1706         if (bits & LA_RDEV)
1707                 inode->i_rdev   = attr->la_rdev;
1708
1709         if (bits & LA_FLAGS) {
1710                 /* always keep S_NOCMTIME */
1711                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1712                                  S_NOCMTIME;
1713         }
1714         return 0;
1715 }
1716
1717 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1718 {
1719         if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1720             (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1721                 struct iattr    iattr;
1722                 int             rc;
1723
1724                 iattr.ia_valid = 0;
1725                 if (attr->la_valid & LA_UID)
1726                         iattr.ia_valid |= ATTR_UID;
1727                 if (attr->la_valid & LA_GID)
1728                         iattr.ia_valid |= ATTR_GID;
1729                 iattr.ia_uid = attr->la_uid;
1730                 iattr.ia_gid = attr->la_gid;
1731
1732                 rc = ll_vfs_dq_transfer(inode, &iattr);
1733                 if (rc) {
1734                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
1735                                "enforcement enabled on the ldiskfs filesystem?",
1736                                inode->i_sb->s_id, rc);
1737                         return rc;
1738                 }
1739         }
1740         return 0;
1741 }
1742
1743 static int osd_attr_set(const struct lu_env *env,
1744                         struct dt_object *dt,
1745                         const struct lu_attr *attr,
1746                         struct thandle *handle,
1747                         struct lustre_capa *capa)
1748 {
1749         struct osd_object *obj = osd_dt_obj(dt);
1750         struct inode      *inode;
1751         int rc;
1752
1753         LASSERT(handle != NULL);
1754         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
1755         LASSERT(osd_invariant(obj));
1756
1757         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1758                 return -EACCES;
1759
1760         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1761
1762         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FID_MAPPING)) {
1763                 struct osd_thread_info  *oti  = osd_oti_get(env);
1764                 const struct lu_fid     *fid0 = lu_object_fid(&dt->do_lu);
1765                 struct lu_fid           *fid1 = &oti->oti_fid;
1766                 struct osd_inode_id     *id   = &oti->oti_id;
1767                 struct iam_path_descr   *ipd;
1768                 struct iam_container    *bag;
1769                 struct osd_thandle      *oh;
1770                 int                      rc;
1771
1772                 fid_cpu_to_be(fid1, fid0);
1773                 memset(id, 1, sizeof(*id));
1774                 bag = &osd_fid2oi(osd_dev(dt->do_lu.lo_dev),
1775                                   fid0)->oi_dir.od_container;
1776                 ipd = osd_idx_ipd_get(env, bag);
1777                 if (unlikely(ipd == NULL))
1778                         RETURN(-ENOMEM);
1779
1780                 oh = container_of0(handle, struct osd_thandle, ot_super);
1781                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)fid1,
1782                                 (const struct iam_rec *)id, ipd);
1783                 osd_ipd_put(env, bag, ipd);
1784                 return(rc > 0 ? 0 : rc);
1785         }
1786
1787         inode = obj->oo_inode;
1788         ll_vfs_dq_init(inode);
1789
1790         rc = osd_quota_transfer(inode, attr);
1791         if (rc)
1792                 return rc;
1793
1794         spin_lock(&obj->oo_guard);
1795         rc = osd_inode_setattr(env, inode, attr);
1796         spin_unlock(&obj->oo_guard);
1797
1798         if (!rc)
1799                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1800         return rc;
1801 }
1802
1803 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1804                                     struct osd_object *obj,
1805                                     const char *name, const int namelen)
1806 {
1807         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1808 }
1809
1810 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1811                       umode_t mode, struct dt_allocation_hint *hint,
1812                       struct thandle *th)
1813 {
1814         int result;
1815         struct osd_device  *osd = osd_obj2dev(obj);
1816         struct osd_thandle *oth;
1817         struct dt_object   *parent = NULL;
1818         struct inode       *inode;
1819
1820         LINVRNT(osd_invariant(obj));
1821         LASSERT(obj->oo_inode == NULL);
1822         LASSERT(obj->oo_hl_head == NULL);
1823
1824         if (S_ISDIR(mode) && ldiskfs_pdo) {
1825                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1826                 if (obj->oo_hl_head == NULL)
1827                         return -ENOMEM;
1828         }
1829
1830         oth = container_of(th, struct osd_thandle, ot_super);
1831         LASSERT(oth->ot_handle->h_transaction != NULL);
1832
1833         if (hint && hint->dah_parent)
1834                 parent = hint->dah_parent;
1835
1836         inode = ldiskfs_create_inode(oth->ot_handle,
1837                                      parent ? osd_dt_obj(parent)->oo_inode :
1838                                               osd_sb(osd)->s_root->d_inode,
1839                                      mode);
1840         if (!IS_ERR(inode)) {
1841                 /* Do not update file c/mtime in ldiskfs.
1842                  * NB: don't need any lock because no contention at this
1843                  * early stage */
1844                 inode->i_flags |= S_NOCMTIME;
1845
1846                 /* For new created object, it must be consistent,
1847                  * and it is unnecessary to scrub against it. */
1848                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1849                 obj->oo_inode = inode;
1850                 result = 0;
1851         } else {
1852                 if (obj->oo_hl_head != NULL) {
1853                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1854                         obj->oo_hl_head = NULL;
1855                 }
1856                 result = PTR_ERR(inode);
1857         }
1858         LINVRNT(osd_invariant(obj));
1859         return result;
1860 }
1861
1862 enum {
1863         OSD_NAME_LEN = 255
1864 };
1865
1866 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1867                      struct lu_attr *attr,
1868                      struct dt_allocation_hint *hint,
1869                      struct dt_object_format *dof,
1870                      struct thandle *th)
1871 {
1872         int result;
1873         struct osd_thandle *oth;
1874         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1875
1876         LASSERT(S_ISDIR(attr->la_mode));
1877
1878         oth = container_of(th, struct osd_thandle, ot_super);
1879         LASSERT(oth->ot_handle->h_transaction != NULL);
1880         result = osd_mkfile(info, obj, mode, hint, th);
1881
1882         return result;
1883 }
1884
1885 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1886                         struct lu_attr *attr,
1887                         struct dt_allocation_hint *hint,
1888                         struct dt_object_format *dof,
1889                         struct thandle *th)
1890 {
1891         int result;
1892         struct osd_thandle *oth;
1893         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1894
1895         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1896
1897         LASSERT(S_ISREG(attr->la_mode));
1898
1899         oth = container_of(th, struct osd_thandle, ot_super);
1900         LASSERT(oth->ot_handle->h_transaction != NULL);
1901
1902         result = osd_mkfile(info, obj, mode, hint, th);
1903         if (result == 0) {
1904                 LASSERT(obj->oo_inode != NULL);
1905                 if (feat->dif_flags & DT_IND_VARKEY)
1906                         result = iam_lvar_create(obj->oo_inode,
1907                                                  feat->dif_keysize_max,
1908                                                  feat->dif_ptrsize,
1909                                                  feat->dif_recsize_max,
1910                                                  oth->ot_handle);
1911                 else
1912                         result = iam_lfix_create(obj->oo_inode,
1913                                                  feat->dif_keysize_max,
1914                                                  feat->dif_ptrsize,
1915                                                  feat->dif_recsize_max,
1916                                                  oth->ot_handle);
1917
1918         }
1919         return result;
1920 }
1921
1922 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1923                      struct lu_attr *attr,
1924                      struct dt_allocation_hint *hint,
1925                      struct dt_object_format *dof,
1926                      struct thandle *th)
1927 {
1928         LASSERT(S_ISREG(attr->la_mode));
1929         return osd_mkfile(info, obj, (attr->la_mode &
1930                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1931 }
1932
1933 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1934                      struct lu_attr *attr,
1935                      struct dt_allocation_hint *hint,
1936                      struct dt_object_format *dof,
1937                      struct thandle *th)
1938 {
1939         LASSERT(S_ISLNK(attr->la_mode));
1940         return osd_mkfile(info, obj, (attr->la_mode &
1941                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1942 }
1943
1944 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1945                      struct lu_attr *attr,
1946                      struct dt_allocation_hint *hint,
1947                      struct dt_object_format *dof,
1948                      struct thandle *th)
1949 {
1950         umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1951         int result;
1952
1953         LINVRNT(osd_invariant(obj));
1954         LASSERT(obj->oo_inode == NULL);
1955         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1956                 S_ISFIFO(mode) || S_ISSOCK(mode));
1957
1958         result = osd_mkfile(info, obj, mode, hint, th);
1959         if (result == 0) {
1960                 LASSERT(obj->oo_inode != NULL);
1961                 /*
1962                  * This inode should be marked dirty for i_rdev.  Currently
1963                  * that is done in the osd_attr_init().
1964                  */
1965                 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1966                                    attr->la_rdev);
1967         }
1968         LINVRNT(osd_invariant(obj));
1969         return result;
1970 }
1971
1972 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1973                               struct lu_attr *,
1974                               struct dt_allocation_hint *hint,
1975                               struct dt_object_format *dof,
1976                               struct thandle *);
1977
1978 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1979 {
1980         osd_obj_type_f result;
1981
1982         switch (type) {
1983         case DFT_DIR:
1984                 result = osd_mkdir;
1985                 break;
1986         case DFT_REGULAR:
1987                 result = osd_mkreg;
1988                 break;
1989         case DFT_SYM:
1990                 result = osd_mksym;
1991                 break;
1992         case DFT_NODE:
1993                 result = osd_mknod;
1994                 break;
1995         case DFT_INDEX:
1996                 result = osd_mk_index;
1997                 break;
1998
1999         default:
2000                 LBUG();
2001                 break;
2002         }
2003         return result;
2004 }
2005
2006
2007 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
2008                         struct dt_object *parent, struct dt_object *child,
2009                         umode_t child_mode)
2010 {
2011         LASSERT(ah);
2012
2013         memset(ah, 0, sizeof(*ah));
2014         ah->dah_parent = parent;
2015         ah->dah_mode = child_mode;
2016 }
2017
2018 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
2019                           struct lu_attr *attr, struct dt_object_format *dof)
2020 {
2021         struct inode   *inode = obj->oo_inode;
2022         __u64           valid = attr->la_valid;
2023         int             result;
2024
2025         attr->la_valid &= ~(LA_TYPE | LA_MODE);
2026
2027         if (dof->dof_type != DFT_NODE)
2028                 attr->la_valid &= ~LA_RDEV;
2029         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
2030                 attr->la_valid &= ~LA_ATIME;
2031         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
2032                 attr->la_valid &= ~LA_CTIME;
2033         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
2034                 attr->la_valid &= ~LA_MTIME;
2035
2036         result = osd_quota_transfer(inode, attr);
2037         if (result)
2038                 return;
2039
2040         if (attr->la_valid != 0) {
2041                 result = osd_inode_setattr(info->oti_env, inode, attr);
2042                 /*
2043                  * The osd_inode_setattr() should always succeed here.  The
2044                  * only error that could be returned is EDQUOT when we are
2045                  * trying to change the UID or GID of the inode. However, this
2046                  * should not happen since quota enforcement is no longer
2047                  * enabled on ldiskfs (lquota takes care of it).
2048                  */
2049                 LASSERTF(result == 0, "%d", result);
2050                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2051         }
2052
2053         attr->la_valid = valid;
2054 }
2055
2056 /**
2057  * Helper function for osd_object_create()
2058  *
2059  * \retval 0, on success
2060  */
2061 static int __osd_object_create(struct osd_thread_info *info,
2062                                struct osd_object *obj, struct lu_attr *attr,
2063                                struct dt_allocation_hint *hint,
2064                                struct dt_object_format *dof,
2065                                struct thandle *th)
2066 {
2067         int     result;
2068         __u32   umask;
2069
2070         /* we drop umask so that permissions we pass are not affected */
2071         umask = current->fs->umask;
2072         current->fs->umask = 0;
2073
2074         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
2075                                                   th);
2076         if (result == 0) {
2077                 osd_attr_init(info, obj, attr, dof);
2078                 osd_object_init0(obj);
2079                 /* bz 24037 */
2080                 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
2081                         unlock_new_inode(obj->oo_inode);
2082         }
2083
2084         /* restore previous umask value */
2085         current->fs->umask = umask;
2086
2087         return result;
2088 }
2089
2090 /**
2091  * Helper function for osd_object_create()
2092  *
2093  * \retval 0, on success
2094  */
2095 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
2096                            const struct lu_fid *fid, struct thandle *th)
2097 {
2098         struct osd_thread_info *info = osd_oti_get(env);
2099         struct osd_inode_id    *id   = &info->oti_id;
2100         struct osd_device      *osd  = osd_obj2dev(obj);
2101
2102         LASSERT(obj->oo_inode != NULL);
2103
2104         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
2105         return osd_oi_insert(info, osd, fid, id, th, OI_CHECK_FLD);
2106 }
2107
2108 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
2109                    obd_seq seq, struct lu_seq_range *range)
2110 {
2111         struct seq_server_site  *ss = osd_seq_site(osd);
2112         int                     rc;
2113
2114         if (fid_seq_is_idif(seq)) {
2115                 fld_range_set_ost(range);
2116                 range->lsr_index = idif_ost_idx(seq);
2117                 return 0;
2118         }
2119
2120         if (!fid_seq_in_fldb(seq)) {
2121                 fld_range_set_mdt(range);
2122                 if (ss != NULL)
2123                         /* FIXME: If ss is NULL, it suppose not get lsr_index
2124                          * at all */
2125                         range->lsr_index = ss->ss_node_id;
2126                 return 0;
2127         }
2128
2129         LASSERT(ss != NULL);
2130         fld_range_set_any(range);
2131         rc = fld_server_lookup(env, ss->ss_server_fld, seq, range);
2132         if (rc != 0) {
2133                 CERROR("%s: cannot find FLD range for "LPX64": rc = %d\n",
2134                        osd_name(osd), seq, rc);
2135         }
2136         return rc;
2137 }
2138
2139 /*
2140  * Concurrency: no external locking is necessary.
2141  */
2142 static int osd_declare_object_create(const struct lu_env *env,
2143                                      struct dt_object *dt,
2144                                      struct lu_attr *attr,
2145                                      struct dt_allocation_hint *hint,
2146                                      struct dt_object_format *dof,
2147                                      struct thandle *handle)
2148 {
2149         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
2150         struct osd_thandle      *oh;
2151         int                      rc;
2152         ENTRY;
2153
2154         LASSERT(handle != NULL);
2155
2156         oh = container_of0(handle, struct osd_thandle, ot_super);
2157         LASSERT(oh->ot_handle == NULL);
2158
2159         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2160                              osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2161         if (!fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2162                            lu_object_fid(&dt->do_lu), OI_CHECK_FLD))
2163                 /* Reuse idle OI block may cause additional one OI block
2164                  * to be changed. */
2165                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2166                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2167
2168         /* If this is directory, then we expect . and .. to be inserted as
2169          * well. The one directory block always needs to be created for the
2170          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2171          * block), there is no danger of needing a tree for the first block.
2172          */
2173         if (attr && S_ISDIR(attr->la_mode)) {
2174                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2175                                      osd_dto_credits_noquota[DTO_WRITE_BASE]);
2176                 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2177         }
2178
2179         if (!attr)
2180                 RETURN(0);
2181
2182         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2183                                    false, false, NULL, false);
2184         if (rc != 0)
2185                 RETURN(rc);
2186
2187         /* It does fld look up inside declare, and the result will be
2188          * added to fld cache, so the following fld lookup inside insert
2189          * does not need send RPC anymore, so avoid send rpc with holding
2190          * transaction */
2191         if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2192                 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2193                 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2194                                fid_seq(lu_object_fid(&dt->do_lu)), range);
2195
2196
2197         RETURN(rc);
2198 }
2199
2200 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2201                              struct lu_attr *attr,
2202                              struct dt_allocation_hint *hint,
2203                              struct dt_object_format *dof,
2204                              struct thandle *th)
2205 {
2206         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2207         struct osd_object      *obj    = osd_dt_obj(dt);
2208         struct osd_thread_info *info   = osd_oti_get(env);
2209         int result;
2210
2211         ENTRY;
2212
2213         LINVRNT(osd_invariant(obj));
2214         LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2215         LASSERT(osd_write_locked(env, obj));
2216         LASSERT(th != NULL);
2217
2218         if (unlikely(fid_is_acct(fid)))
2219                 /* Quota files can't be created from the kernel any more,
2220                  * 'tune2fs -O quota' will take care of creating them */
2221                 RETURN(-EPERM);
2222
2223         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2224         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2225
2226         result = __osd_object_create(info, obj, attr, hint, dof, th);
2227         if (result == 0)
2228                 result = __osd_oi_insert(env, obj, fid, th);
2229
2230         LASSERT(ergo(result == 0,
2231                      dt_object_exists(dt) && !dt_object_remote(dt)));
2232
2233         LASSERT(osd_invariant(obj));
2234         RETURN(result);
2235 }
2236
2237 /**
2238  * Called to destroy on-disk representation of the object
2239  *
2240  * Concurrency: must be locked
2241  */
2242 static int osd_declare_object_destroy(const struct lu_env *env,
2243                                       struct dt_object *dt,
2244                                       struct thandle *th)
2245 {
2246         struct osd_object  *obj = osd_dt_obj(dt);
2247         struct inode       *inode = obj->oo_inode;
2248         struct osd_thandle *oh;
2249         int                 rc;
2250         ENTRY;
2251
2252         oh = container_of0(th, struct osd_thandle, ot_super);
2253         LASSERT(oh->ot_handle == NULL);
2254         LASSERT(inode);
2255
2256         osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2257                              osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2258         /* Recycle idle OI leaf may cause additional three OI blocks
2259          * to be changed. */
2260         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2261                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3);
2262         /* one less inode */
2263         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2264                                    false, true, NULL, false);
2265         if (rc)
2266                 RETURN(rc);
2267         /* data to be truncated */
2268         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2269                                    true, true, NULL, false);
2270         RETURN(rc);
2271 }
2272
2273 static int osd_object_destroy(const struct lu_env *env,
2274                               struct dt_object *dt,
2275                               struct thandle *th)
2276 {
2277         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2278         struct osd_object      *obj = osd_dt_obj(dt);
2279         struct inode           *inode = obj->oo_inode;
2280         struct osd_device      *osd = osd_obj2dev(obj);
2281         struct osd_thandle     *oh;
2282         int                     result;
2283         ENTRY;
2284
2285         oh = container_of0(th, struct osd_thandle, ot_super);
2286         LASSERT(oh->ot_handle);
2287         LASSERT(inode);
2288         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2289
2290         if (unlikely(fid_is_acct(fid)))
2291                 RETURN(-EPERM);
2292
2293         if (S_ISDIR(inode->i_mode)) {
2294                 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2295                 /* it will check/delete the inode from remote parent,
2296                  * how to optimize it? unlink performance impaction XXX */
2297                 result = osd_delete_from_remote_parent(env, osd, obj, oh);
2298                 if (result != 0 && result != -ENOENT) {
2299                         CERROR("%s: delete inode "DFID": rc = %d\n",
2300                                osd_name(osd), PFID(fid), result);
2301                 }
2302                 spin_lock(&obj->oo_guard);
2303                 clear_nlink(inode);
2304                 spin_unlock(&obj->oo_guard);
2305                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2306         }
2307
2308         osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2309
2310         result = osd_oi_delete(osd_oti_get(env), osd, fid, th, OI_CHECK_FLD);
2311
2312         /* XXX: add to ext3 orphan list */
2313         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2314
2315         /* not needed in the cache anymore */
2316         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2317
2318         RETURN(0);
2319 }
2320
2321 /**
2322  * Put the fid into lustre_mdt_attrs, and then place the structure
2323  * inode's ea. This fid should not be altered during the life time
2324  * of the inode.
2325  *
2326  * \retval +ve, on success
2327  * \retval -ve, on error
2328  *
2329  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2330  */
2331 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2332                    const struct lu_fid *fid, __u32 compat, __u32 incompat)
2333 {
2334         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2335         int                      rc;
2336         ENTRY;
2337
2338         if (OBD_FAIL_CHECK(OBD_FAIL_FID_INLMA))
2339                 RETURN(0);
2340
2341         lustre_lma_init(lma, fid, compat, incompat);
2342         lustre_lma_swab(lma);
2343
2344         rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2345                              XATTR_CREATE);
2346         /* LMA may already exist, but we need to check that all the
2347          * desired compat/incompat flags have been added. */
2348         if (unlikely(rc == -EEXIST)) {
2349                 if (compat == 0 && incompat == 0)
2350                         RETURN(0);
2351
2352                 rc = __osd_xattr_get(inode, &info->oti_obj_dentry,
2353                                      XATTR_NAME_LMA, info->oti_mdt_attrs_old,
2354                                      LMA_OLD_SIZE);
2355                 if (rc <= 0)
2356                         RETURN(-EINVAL);
2357
2358                 lustre_lma_swab(lma);
2359                 if (!(~lma->lma_compat & compat) &&
2360                     !(~lma->lma_incompat & incompat))
2361                         RETURN(0);
2362
2363                 lma->lma_compat |= compat;
2364                 lma->lma_incompat |= incompat;
2365                 lustre_lma_swab(lma);
2366                 rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma,
2367                                      sizeof(*lma), XATTR_REPLACE);
2368         }
2369
2370         RETURN(rc);
2371 }
2372
2373 /**
2374  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2375  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2376  * To have compatilibility with 1.8 ldiskfs driver we need to have
2377  * magic number at start of fid data.
2378  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2379  * its inmemory API.
2380  */
2381 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2382                                   const struct dt_rec *fid)
2383 {
2384         if (!fid_is_namespace_visible((const struct lu_fid *)fid) ||
2385             OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF)) {
2386                 param->edp_magic = 0;
2387                 return;
2388         }
2389
2390         param->edp_magic = LDISKFS_LUFID_MAGIC;
2391         param->edp_len =  sizeof(struct lu_fid) + 1;
2392         fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2393 }
2394
2395 /**
2396  * Try to read the fid from inode ea into dt_rec.
2397  *
2398  * \param fid object fid.
2399  *
2400  * \retval 0 on success
2401  */
2402 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2403                           __u32 ino, struct lu_fid *fid,
2404                           struct osd_inode_id *id)
2405 {
2406         struct osd_thread_info *info  = osd_oti_get(env);
2407         struct inode           *inode;
2408         ENTRY;
2409
2410         osd_id_gen(id, ino, OSD_OII_NOGEN);
2411         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2412         if (IS_ERR(inode))
2413                 RETURN(PTR_ERR(inode));
2414
2415         iput(inode);
2416         RETURN(0);
2417 }
2418
2419 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2420                                         struct inode *dir,
2421                                         struct inode  *parent_dir,
2422                                         const struct dt_rec *dot_fid,
2423                                         const struct dt_rec *dot_dot_fid,
2424                                         struct osd_thandle *oth)
2425 {
2426         struct ldiskfs_dentry_param *dot_ldp;
2427         struct ldiskfs_dentry_param *dot_dot_ldp;
2428
2429         dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2430         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2431
2432         dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2433         dot_ldp->edp_magic = 0;
2434         return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2435                                         dir, dot_ldp, dot_dot_ldp);
2436 }
2437
2438 /**
2439  * Create an local agent inode for remote entry
2440  */
2441 static struct inode *osd_create_local_agent_inode(const struct lu_env *env,
2442                                                   struct osd_device *osd,
2443                                                   struct osd_object *pobj,
2444                                                   const struct lu_fid *fid,
2445                                                   struct thandle *th)
2446 {
2447         struct osd_thread_info  *info = osd_oti_get(env);
2448         struct inode            *local;
2449         struct osd_thandle      *oh;
2450         int                     rc;
2451         ENTRY;
2452
2453         LASSERT(th);
2454         oh = container_of(th, struct osd_thandle, ot_super);
2455         LASSERT(oh->ot_handle->h_transaction != NULL);
2456
2457         /* FIXME: Insert index api needs to know the mode of
2458          * the remote object. Just use S_IFDIR for now */
2459         local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2460         if (IS_ERR(local)) {
2461                 CERROR("%s: create local error %d\n", osd_name(osd),
2462                        (int)PTR_ERR(local));
2463                 RETURN(local);
2464         }
2465
2466         /* Set special LMA flag for local agent inode */
2467         rc = osd_ea_fid_set(info, local, fid, 0, LMAI_AGENT);
2468         if (rc != 0) {
2469                 CERROR("%s: set LMA for "DFID" remote inode failed: rc = %d\n",
2470                        osd_name(osd), PFID(fid), rc);
2471                 RETURN(ERR_PTR(rc));
2472         }
2473
2474         rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2475                 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2476                 (const struct dt_rec *)fid, oh);
2477         if (rc != 0) {
2478                 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2479                         osd_name(osd), PFID(fid), rc);
2480                 RETURN(ERR_PTR(rc));
2481         }
2482
2483         RETURN(local);
2484 }
2485
2486 /**
2487  * Delete local agent inode for remote entry
2488  */
2489 static int osd_delete_local_agent_inode(const struct lu_env *env,
2490                                         struct osd_device *osd,
2491                                         const struct lu_fid *fid,
2492                                         __u32 ino, struct osd_thandle *oh)
2493 {
2494         struct osd_thread_info  *oti = osd_oti_get(env);
2495         struct osd_inode_id     *id = &oti->oti_id;
2496         struct inode            *inode;
2497         ENTRY;
2498
2499         id->oii_ino = le32_to_cpu(ino);
2500         id->oii_gen = OSD_OII_NOGEN;
2501         inode = osd_iget(oti, osd, id);
2502         if (IS_ERR(inode)) {
2503                 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2504                        PFID(fid), id->oii_ino, id->oii_gen);
2505                 RETURN(PTR_ERR(inode));
2506         }
2507
2508         clear_nlink(inode);
2509         mark_inode_dirty(inode);
2510         CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2511                 osd_name(osd), PFID(fid), inode->i_ino);
2512         iput(inode);
2513         RETURN(0);
2514 }
2515
2516 /**
2517  * OSD layer object create function for interoperability mode (b11826).
2518  * This is mostly similar to osd_object_create(). Only difference being, fid is
2519  * inserted into inode ea here.
2520  *
2521  * \retval   0, on success
2522  * \retval -ve, on error
2523  */
2524 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2525                                 struct lu_attr *attr,
2526                                 struct dt_allocation_hint *hint,
2527                                 struct dt_object_format *dof,
2528                                 struct thandle *th)
2529 {
2530         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2531         struct osd_object      *obj    = osd_dt_obj(dt);
2532         struct osd_thread_info *info   = osd_oti_get(env);
2533         int                     result;
2534
2535         ENTRY;
2536
2537         LASSERT(osd_invariant(obj));
2538         LASSERT(!dt_object_exists(dt) && !dt_object_remote(dt));
2539         LASSERT(osd_write_locked(env, obj));
2540         LASSERT(th != NULL);
2541
2542         if (unlikely(fid_is_acct(fid)))
2543                 /* Quota files can't be created from the kernel any more,
2544                  * 'tune2fs -O quota' will take care of creating them */
2545                 RETURN(-EPERM);
2546
2547         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2548         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2549
2550         result = __osd_object_create(info, obj, attr, hint, dof, th);
2551         if (result == 0)
2552                 result = osd_ea_fid_set(info, obj->oo_inode, fid,
2553                                 fid_is_on_ost(info, osd_obj2dev(obj),
2554                                               fid, OI_CHECK_FLD) ?
2555                                 LMAC_FID_ON_OST : 0, 0);
2556
2557         if (result == 0)
2558                 result = __osd_oi_insert(env, obj, fid, th);
2559
2560         LASSERT(ergo(result == 0,
2561                      dt_object_exists(dt) && !dt_object_remote(dt)));
2562         LINVRNT(osd_invariant(obj));
2563         RETURN(result);
2564 }
2565
2566 static int osd_declare_object_ref_add(const struct lu_env *env,
2567                                       struct dt_object *dt,
2568                                       struct thandle *handle)
2569 {
2570         struct osd_thandle       *oh;
2571
2572         /* it's possible that object doesn't exist yet */
2573         LASSERT(handle != NULL);
2574
2575         oh = container_of0(handle, struct osd_thandle, ot_super);
2576         LASSERT(oh->ot_handle == NULL);
2577
2578         osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2579                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2580
2581         return 0;
2582 }
2583
2584 /*
2585  * Concurrency: @dt is write locked.
2586  */
2587 static int osd_object_ref_add(const struct lu_env *env,
2588                               struct dt_object *dt, struct thandle *th)
2589 {
2590         struct osd_object *obj = osd_dt_obj(dt);
2591         struct inode      *inode = obj->oo_inode;
2592         bool               need_dirty = false;
2593         int                rc = 0;
2594
2595         LINVRNT(osd_invariant(obj));
2596         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2597         LASSERT(osd_write_locked(env, obj));
2598         LASSERT(th != NULL);
2599
2600         osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2601
2602         /* This based on ldiskfs_inc_count(), which is not exported.
2603          *
2604          * The DIR_NLINK feature allows directories to exceed LDISKFS_LINK_MAX
2605          * (65000) subdirectories by storing "1" in i_nlink if the link count
2606          * would otherwise overflow. Directory tranversal tools understand
2607          * that (st_nlink == 1) indicates that the filesystem dose not track
2608          * hard links count on the directory, and will not abort subdirectory
2609          * scanning early once (st_nlink - 2) subdirs have been found.
2610          *
2611          * This also has to properly handle the case of inodes with nlink == 0
2612          * in case they are being linked into the PENDING directory
2613          */
2614         spin_lock(&obj->oo_guard);
2615         if (unlikely(!S_ISDIR(inode->i_mode) &&
2616                      inode->i_nlink >= LDISKFS_LINK_MAX)) {
2617                 /* MDD should have checked this, but good to be safe */
2618                 rc = -EMLINK;
2619         } else if (unlikely(inode->i_nlink == 0 ||
2620                             (S_ISDIR(inode->i_mode) &&
2621                              inode->i_nlink >= LDISKFS_LINK_MAX))) {
2622                 /* inc_nlink from 0 may cause WARN_ON */
2623                 set_nlink(inode, 1);
2624                 need_dirty = true;
2625         } else if (!S_ISDIR(inode->i_mode) ||
2626                    (S_ISDIR(inode->i_mode) && inode->i_nlink >= 2)) {
2627                 inc_nlink(inode);
2628                 need_dirty = true;
2629         } /* else (S_ISDIR(inode->i_mode) && inode->i_nlink == 1) { ; } */
2630
2631         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2632         spin_unlock(&obj->oo_guard);
2633
2634         if (need_dirty)
2635                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2636
2637         LINVRNT(osd_invariant(obj));
2638
2639         return rc;
2640 }
2641
2642 static int osd_declare_object_ref_del(const struct lu_env *env,
2643                                       struct dt_object *dt,
2644                                       struct thandle *handle)
2645 {
2646         struct osd_thandle *oh;
2647
2648         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2649         LASSERT(handle != NULL);
2650
2651         oh = container_of0(handle, struct osd_thandle, ot_super);
2652         LASSERT(oh->ot_handle == NULL);
2653
2654         osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2655                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2656
2657         return 0;
2658 }
2659
2660 /*
2661  * Concurrency: @dt is write locked.
2662  */
2663 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2664                               struct thandle *th)
2665 {
2666         struct osd_object       *obj = osd_dt_obj(dt);
2667         struct inode            *inode = obj->oo_inode;
2668         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
2669
2670         LINVRNT(osd_invariant(obj));
2671         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2672         LASSERT(osd_write_locked(env, obj));
2673         LASSERT(th != NULL);
2674
2675         osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2676
2677         spin_lock(&obj->oo_guard);
2678         /* That can be result of upgrade from old Lustre version and
2679          * applied only to local files.  Just skip this ref_del call.
2680          * ext4_unlink() only treats this as a warning, don't LASSERT here.*/
2681         if (inode->i_nlink == 0) {
2682                 CDEBUG_LIMIT(fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2683                              D_ERROR : D_INODE, "%s: nlink == 0 on "DFID
2684                              ", maybe an upgraded file? (LU-3915)\n",
2685                              osd_name(osd), PFID(lu_object_fid(&dt->do_lu)));
2686                 spin_unlock(&obj->oo_guard);
2687                 return 0;
2688         }
2689
2690         /* This based on ldiskfs_dec_count(), which is not exported.
2691          *
2692          * If a directory already has nlink == 1, then do not drop the nlink
2693          * count to 0, even temporarily, to avoid race conditions with other
2694          * threads not holding oo_guard seeing i_nlink == 0 in rare cases.
2695          *
2696          * nlink == 1 means the directory has/had > EXT4_LINK_MAX subdirs.
2697          */
2698         if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 1) {
2699                 drop_nlink(inode);
2700
2701                 spin_unlock(&obj->oo_guard);
2702                 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2703                 LINVRNT(osd_invariant(obj));
2704         } else {
2705                 spin_unlock(&obj->oo_guard);
2706         }
2707
2708         return 0;
2709 }
2710
2711 /*
2712  * Get the 64-bit version for an inode.
2713  */
2714 static int osd_object_version_get(const struct lu_env *env,
2715                                   struct dt_object *dt, dt_obj_version_t *ver)
2716 {
2717         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2718
2719         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2720                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2721         *ver = LDISKFS_I(inode)->i_fs_version;
2722         return 0;
2723 }
2724
2725 /*
2726  * Concurrency: @dt is read locked.
2727  */
2728 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2729                          struct lu_buf *buf, const char *name,
2730                          struct lustre_capa *capa)
2731 {
2732         struct osd_object      *obj    = osd_dt_obj(dt);
2733         struct inode           *inode  = obj->oo_inode;
2734         struct osd_thread_info *info   = osd_oti_get(env);
2735         struct dentry          *dentry = &info->oti_obj_dentry;
2736
2737         /* version get is not real XATTR but uses xattr API */
2738         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2739                 /* for version we are just using xattr API but change inode
2740                  * field instead */
2741                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2742                 osd_object_version_get(env, dt, buf->lb_buf);
2743                 return sizeof(dt_obj_version_t);
2744         }
2745
2746         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2747         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2748
2749         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2750                 return -EACCES;
2751
2752         return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2753 }
2754
2755
2756 static int osd_declare_xattr_set(const struct lu_env *env,
2757                                  struct dt_object *dt,
2758                                  const struct lu_buf *buf, const char *name,
2759                                  int fl, struct thandle *handle)
2760 {
2761         struct osd_thandle *oh;
2762
2763         LASSERT(handle != NULL);
2764
2765         oh = container_of0(handle, struct osd_thandle, ot_super);
2766         LASSERT(oh->ot_handle == NULL);
2767
2768         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2769                              strcmp(name, XATTR_NAME_VERSION) == 0 ?
2770                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2771                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2772
2773         return 0;
2774 }
2775
2776 /*
2777  * Set the 64-bit version for object
2778  */
2779 static void osd_object_version_set(const struct lu_env *env,
2780                                    struct dt_object *dt,
2781                                    dt_obj_version_t *new_version)
2782 {
2783         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2784
2785         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2786                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2787
2788         LDISKFS_I(inode)->i_fs_version = *new_version;
2789         /** Version is set after all inode operations are finished,
2790          *  so we should mark it dirty here */
2791         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
2792 }
2793
2794 /*
2795  * Concurrency: @dt is write locked.
2796  */
2797 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2798                          const struct lu_buf *buf, const char *name, int fl,
2799                          struct thandle *handle, struct lustre_capa *capa)
2800 {
2801         struct osd_object      *obj      = osd_dt_obj(dt);
2802         struct inode           *inode    = obj->oo_inode;
2803         struct osd_thread_info *info     = osd_oti_get(env);
2804         int                     fs_flags = 0;
2805         ENTRY;
2806
2807         LASSERT(handle != NULL);
2808
2809         /* version set is not real XATTR */
2810         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2811                 /* for version we are just using xattr API but change inode
2812                  * field instead */
2813                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2814                 osd_object_version_set(env, dt, buf->lb_buf);
2815                 return sizeof(dt_obj_version_t);
2816         }
2817
2818         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2819                 return -EACCES;
2820
2821         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2822         if (fl & LU_XATTR_REPLACE)
2823                 fs_flags |= XATTR_REPLACE;
2824
2825         if (fl & LU_XATTR_CREATE)
2826                 fs_flags |= XATTR_CREATE;
2827
2828         return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2829                                fs_flags);
2830 }
2831
2832 /*
2833  * Concurrency: @dt is read locked.
2834  */
2835 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2836                           struct lu_buf *buf, struct lustre_capa *capa)
2837 {
2838         struct osd_object      *obj    = osd_dt_obj(dt);
2839         struct inode           *inode  = obj->oo_inode;
2840         struct osd_thread_info *info   = osd_oti_get(env);
2841         struct dentry          *dentry = &info->oti_obj_dentry;
2842
2843         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2844         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2845         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2846
2847         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2848                 return -EACCES;
2849
2850         dentry->d_inode = inode;
2851         dentry->d_sb = inode->i_sb;
2852         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2853 }
2854
2855 static int osd_declare_xattr_del(const struct lu_env *env,
2856                                  struct dt_object *dt, const char *name,
2857                                  struct thandle *handle)
2858 {
2859         struct osd_thandle *oh;
2860
2861         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2862         LASSERT(handle != NULL);
2863
2864         oh = container_of0(handle, struct osd_thandle, ot_super);
2865         LASSERT(oh->ot_handle == NULL);
2866
2867         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2868                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2869
2870         return 0;
2871 }
2872
2873 /*
2874  * Concurrency: @dt is write locked.
2875  */
2876 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2877                          const char *name, struct thandle *handle,
2878                          struct lustre_capa *capa)
2879 {
2880         struct osd_object      *obj    = osd_dt_obj(dt);
2881         struct inode           *inode  = obj->oo_inode;
2882         struct osd_thread_info *info   = osd_oti_get(env);
2883         struct dentry          *dentry = &info->oti_obj_dentry;
2884         int                     rc;
2885
2886         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2887         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2888         LASSERT(handle != NULL);
2889
2890         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2891                 return -EACCES;
2892
2893         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2894
2895         ll_vfs_dq_init(inode);
2896         dentry->d_inode = inode;
2897         dentry->d_sb = inode->i_sb;
2898         rc = inode->i_op->removexattr(dentry, name);
2899         return rc;
2900 }
2901
2902 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2903                                      struct dt_object *dt,
2904                                      struct lustre_capa *old, __u64 opc)
2905 {
2906         struct osd_thread_info *info = osd_oti_get(env);
2907         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2908         struct osd_object *obj = osd_dt_obj(dt);
2909         struct osd_device *osd = osd_obj2dev(obj);
2910         struct lustre_capa_key *key = &info->oti_capa_key;
2911         struct lustre_capa *capa = &info->oti_capa;
2912         struct obd_capa *oc;
2913         struct lu_capainfo *lci;
2914         int rc;
2915         ENTRY;
2916
2917         if (!osd->od_fl_capa)
2918                 RETURN(ERR_PTR(-ENOENT));
2919
2920         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
2921         LINVRNT(osd_invariant(obj));
2922
2923         /* renewal sanity check */
2924         if (old && osd_object_auth(env, dt, old, opc))
2925                 RETURN(ERR_PTR(-EACCES));
2926
2927         lci = lu_capainfo_get(env);
2928         if (unlikely(lci == NULL))
2929                 RETURN(ERR_PTR(-ENOENT));
2930
2931         switch (lci->lci_auth) {
2932         case LC_ID_NONE:
2933                 RETURN(NULL);
2934         case LC_ID_PLAIN:
2935                 capa->lc_uid = obj->oo_inode->i_uid;
2936                 capa->lc_gid = obj->oo_inode->i_gid;
2937                 capa->lc_flags = LC_ID_PLAIN;
2938                 break;
2939         case LC_ID_CONVERT: {
2940                 __u32 d[4], s[4];
2941
2942                 s[0] = obj->oo_inode->i_uid;
2943                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2944                 s[2] = obj->oo_inode->i_gid;
2945                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2946                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2947                 if (unlikely(rc))
2948                         RETURN(ERR_PTR(rc));
2949
2950                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2951                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2952                 capa->lc_flags = LC_ID_CONVERT;
2953                 break;
2954         }
2955         default:
2956                 RETURN(ERR_PTR(-EINVAL));
2957         }
2958
2959         capa->lc_fid = *fid;
2960         capa->lc_opc = opc;
2961         capa->lc_flags |= osd->od_capa_alg << 24;
2962         capa->lc_timeout = osd->od_capa_timeout;
2963         capa->lc_expiry = 0;
2964
2965         oc = capa_lookup(osd->od_capa_hash, capa, 1);
2966         if (oc) {
2967                 LASSERT(!capa_is_expired(oc));
2968                 RETURN(oc);
2969         }
2970
2971         spin_lock(&capa_lock);
2972         *key = osd->od_capa_keys[1];
2973         spin_unlock(&capa_lock);
2974
2975         capa->lc_keyid = key->lk_keyid;
2976         capa->lc_expiry = cfs_time_current_sec() + osd->od_capa_timeout;
2977
2978         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2979         if (rc) {
2980                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2981                 RETURN(ERR_PTR(rc));
2982         }
2983
2984         oc = capa_add(osd->od_capa_hash, capa);
2985         RETURN(oc);
2986 }
2987
2988 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2989 {
2990         struct osd_object       *obj    = osd_dt_obj(dt);
2991         struct inode            *inode  = obj->oo_inode;
2992         struct osd_thread_info  *info   = osd_oti_get(env);
2993         struct dentry           *dentry = &info->oti_obj_dentry;
2994         struct file             *file   = &info->oti_file;
2995         int                     rc;
2996
2997         ENTRY;
2998
2999         dentry->d_inode = inode;
3000         dentry->d_sb = inode->i_sb;
3001         file->f_dentry = dentry;
3002         file->f_mapping = inode->i_mapping;
3003         file->f_op = inode->i_fop;
3004 #ifndef HAVE_FILE_FSYNC_4ARGS
3005         mutex_lock(&inode->i_mutex);
3006 #endif
3007         rc = do_fsync(file, 0);
3008 #ifndef HAVE_FILE_FSYNC_4ARGS
3009         mutex_unlock(&inode->i_mutex);
3010 #endif
3011         RETURN(rc);
3012 }
3013
3014 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
3015                         void **data)
3016 {
3017         struct osd_object *obj = osd_dt_obj(dt);
3018         ENTRY;
3019
3020         *data = (void *)obj->oo_inode;
3021         RETURN(0);
3022 }
3023
3024 /*
3025  * Index operations.
3026  */
3027
3028 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
3029                            const struct dt_index_features *feat)
3030 {
3031         struct iam_descr *descr;
3032
3033         if (osd_object_is_root(o))
3034                 return feat == &dt_directory_features;
3035
3036         LASSERT(o->oo_dir != NULL);
3037
3038         descr = o->oo_dir->od_container.ic_descr;
3039         if (feat == &dt_directory_features) {
3040                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
3041                         return 1;
3042                 else
3043                         return 0;
3044         } else {
3045                 return
3046                         feat->dif_keysize_min <= descr->id_key_size &&
3047                         descr->id_key_size <= feat->dif_keysize_max &&
3048                         feat->dif_recsize_min <= descr->id_rec_size &&
3049                         descr->id_rec_size <= feat->dif_recsize_max &&
3050                         !(feat->dif_flags & (DT_IND_VARKEY |
3051                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
3052                         ergo(feat->dif_flags & DT_IND_UPDATE,
3053                              1 /* XXX check that object (and file system) is
3054                                 * writable */);
3055         }
3056 }
3057
3058 static int osd_iam_container_init(const struct lu_env *env,
3059                                   struct osd_object *obj,
3060                                   struct osd_directory *dir)
3061 {
3062         struct iam_container *bag = &dir->od_container;
3063         int result;
3064
3065         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
3066         if (result != 0)
3067                 return result;
3068
3069         result = iam_container_setup(bag);
3070         if (result == 0)
3071                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
3072         else
3073                 iam_container_fini(bag);
3074
3075         return result;
3076 }
3077
3078
3079 /*
3080  * Concurrency: no external locking is necessary.
3081  */
3082 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
3083                          const struct dt_index_features *feat)
3084 {
3085         int                      result;
3086         int                      skip_iam = 0;
3087         struct osd_object       *obj = osd_dt_obj(dt);
3088
3089         LINVRNT(osd_invariant(obj));
3090
3091         if (osd_object_is_root(obj)) {
3092                 dt->do_index_ops = &osd_index_ea_ops;
3093                 result = 0;
3094         } else if (feat == &dt_directory_features) {
3095                 dt->do_index_ops = &osd_index_ea_ops;
3096                 if (obj->oo_inode != NULL && S_ISDIR(obj->oo_inode->i_mode))
3097                         result = 0;
3098                 else
3099                         result = -ENOTDIR;
3100                 skip_iam = 1;
3101         } else if (unlikely(feat == &dt_otable_features)) {
3102                 dt->do_index_ops = &osd_otable_ops;
3103                 return 0;
3104         } else if (unlikely(feat == &dt_acct_features)) {
3105                 dt->do_index_ops = &osd_acct_index_ops;
3106                 result = 0;
3107                 skip_iam = 1;
3108         } else if (!osd_has_index(obj)) {
3109                 struct osd_directory *dir;
3110
3111                 OBD_ALLOC_PTR(dir);
3112                 if (dir != NULL) {
3113
3114                         spin_lock(&obj->oo_guard);
3115                         if (obj->oo_dir == NULL)
3116                                 obj->oo_dir = dir;
3117                         else
3118                                 /*
3119                                  * Concurrent thread allocated container data.
3120                                  */
3121                                 OBD_FREE_PTR(dir);
3122                         spin_unlock(&obj->oo_guard);
3123                         /*
3124                          * Now, that we have container data, serialize its
3125                          * initialization.
3126                          */
3127                         down_write(&obj->oo_ext_idx_sem);
3128                         /*
3129                          * recheck under lock.
3130                          */
3131                         if (!osd_has_index(obj))
3132                                 result = osd_iam_container_init(env, obj, dir);
3133                         else
3134                                 result = 0;
3135                         up_write(&obj->oo_ext_idx_sem);
3136                 } else {
3137                         result = -ENOMEM;
3138                 }
3139         } else {
3140                 result = 0;
3141         }
3142
3143         if (result == 0 && skip_iam == 0) {
3144                 if (!osd_iam_index_probe(env, obj, feat))
3145                         result = -ENOTDIR;
3146         }
3147         LINVRNT(osd_invariant(obj));
3148
3149         if (result == 0 && is_quota_glb_feat(feat) &&
3150             fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB)
3151                 result = osd_quota_migration(env, dt, feat);
3152
3153         return result;
3154 }
3155
3156 static int osd_otable_it_attr_get(const struct lu_env *env,
3157                                  struct dt_object *dt,
3158                                  struct lu_attr *attr,
3159                                  struct lustre_capa *capa)
3160 {
3161         attr->la_valid = 0;
3162         return 0;
3163 }
3164
3165 static const struct dt_object_operations osd_obj_ops = {
3166         .do_read_lock         = osd_object_read_lock,
3167         .do_write_lock        = osd_object_write_lock,
3168         .do_read_unlock       = osd_object_read_unlock,
3169         .do_write_unlock      = osd_object_write_unlock,
3170         .do_write_locked      = osd_object_write_locked,
3171         .do_attr_get          = osd_attr_get,
3172         .do_declare_attr_set  = osd_declare_attr_set,
3173         .do_attr_set          = osd_attr_set,
3174         .do_ah_init           = osd_ah_init,
3175         .do_declare_create    = osd_declare_object_create,
3176         .do_create            = osd_object_create,
3177         .do_declare_destroy   = osd_declare_object_destroy,
3178         .do_destroy           = osd_object_destroy,
3179         .do_index_try         = osd_index_try,
3180         .do_declare_ref_add   = osd_declare_object_ref_add,
3181         .do_ref_add           = osd_object_ref_add,
3182         .do_declare_ref_del   = osd_declare_object_ref_del,
3183         .do_ref_del           = osd_object_ref_del,
3184         .do_xattr_get         = osd_xattr_get,
3185         .do_declare_xattr_set = osd_declare_xattr_set,
3186         .do_xattr_set         = osd_xattr_set,
3187         .do_declare_xattr_del = osd_declare_xattr_del,
3188         .do_xattr_del         = osd_xattr_del,
3189         .do_xattr_list        = osd_xattr_list,
3190         .do_capa_get          = osd_capa_get,
3191         .do_object_sync       = osd_object_sync,
3192         .do_data_get          = osd_data_get,
3193 };
3194
3195 /**
3196  * dt_object_operations for interoperability mode
3197  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
3198  */
3199 static const struct dt_object_operations osd_obj_ea_ops = {
3200         .do_read_lock         = osd_object_read_lock,
3201         .do_write_lock        = osd_object_write_lock,
3202         .do_read_unlock       = osd_object_read_unlock,
3203         .do_write_unlock      = osd_object_write_unlock,
3204         .do_write_locked      = osd_object_write_locked,
3205         .do_attr_get          = osd_attr_get,
3206         .do_declare_attr_set  = osd_declare_attr_set,
3207         .do_attr_set          = osd_attr_set,
3208         .do_ah_init           = osd_ah_init,
3209         .do_declare_create    = osd_declare_object_create,
3210         .do_create            = osd_object_ea_create,
3211         .do_declare_destroy   = osd_declare_object_destroy,
3212         .do_destroy           = osd_object_destroy,
3213         .do_index_try         = osd_index_try,
3214         .do_declare_ref_add   = osd_declare_object_ref_add,
3215         .do_ref_add           = osd_object_ref_add,
3216         .do_declare_ref_del   = osd_declare_object_ref_del,
3217         .do_ref_del           = osd_object_ref_del,
3218         .do_xattr_get         = osd_xattr_get,
3219         .do_declare_xattr_set = osd_declare_xattr_set,
3220         .do_xattr_set         = osd_xattr_set,
3221         .do_declare_xattr_del = osd_declare_xattr_del,
3222         .do_xattr_del         = osd_xattr_del,
3223         .do_xattr_list        = osd_xattr_list,
3224         .do_capa_get          = osd_capa_get,
3225         .do_object_sync       = osd_object_sync,
3226         .do_data_get          = osd_data_get,
3227 };
3228
3229 static const struct dt_object_operations osd_obj_otable_it_ops = {
3230         .do_attr_get    = osd_otable_it_attr_get,
3231         .do_index_try   = osd_index_try,
3232 };
3233
3234 static int osd_index_declare_iam_delete(const struct lu_env *env,
3235                                         struct dt_object *dt,
3236                                         const struct dt_key *key,
3237                                         struct thandle *handle)
3238 {
3239         struct osd_thandle    *oh;
3240
3241         oh = container_of0(handle, struct osd_thandle, ot_super);
3242         LASSERT(oh->ot_handle == NULL);
3243
3244         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3245                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3246
3247         return 0;
3248 }
3249
3250 /**
3251  *      delete a (key, value) pair from index \a dt specified by \a key
3252  *
3253  *      \param  dt      osd index object
3254  *      \param  key     key for index
3255  *      \param  rec     record reference
3256  *      \param  handle  transaction handler
3257  *
3258  *      \retval  0  success
3259  *      \retval -ve   failure
3260  */
3261
3262 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3263                                 const struct dt_key *key,
3264                                 struct thandle *handle,
3265                                 struct lustre_capa *capa)
3266 {
3267         struct osd_thread_info *oti = osd_oti_get(env);
3268         struct osd_object      *obj = osd_dt_obj(dt);
3269         struct osd_thandle     *oh;
3270         struct iam_path_descr  *ipd;
3271         struct iam_container   *bag = &obj->oo_dir->od_container;
3272         int                     rc;
3273
3274         ENTRY;
3275
3276         LINVRNT(osd_invariant(obj));
3277         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3278         LASSERT(bag->ic_object == obj->oo_inode);
3279         LASSERT(handle != NULL);
3280
3281         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3282                 RETURN(-EACCES);
3283
3284         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3285
3286         ipd = osd_idx_ipd_get(env, bag);
3287         if (unlikely(ipd == NULL))
3288                 RETURN(-ENOMEM);
3289
3290         oh = container_of0(handle, struct osd_thandle, ot_super);
3291         LASSERT(oh->ot_handle != NULL);
3292         LASSERT(oh->ot_handle->h_transaction != NULL);
3293
3294         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3295                 /* swab quota uid/gid provided by caller */
3296                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3297                 key = (const struct dt_key *)&oti->oti_quota_id;
3298         }
3299
3300         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3301         osd_ipd_put(env, bag, ipd);
3302         LINVRNT(osd_invariant(obj));
3303         RETURN(rc);
3304 }
3305
3306 static int osd_index_declare_ea_delete(const struct lu_env *env,
3307                                        struct dt_object *dt,
3308                                        const struct dt_key *key,
3309                                        struct thandle *handle)
3310 {
3311         struct osd_thandle *oh;
3312         struct inode       *inode;
3313         int                 rc;
3314         ENTRY;
3315
3316         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3317         LASSERT(handle != NULL);
3318
3319         oh = container_of0(handle, struct osd_thandle, ot_super);
3320         LASSERT(oh->ot_handle == NULL);
3321
3322         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3323                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3324
3325         inode = osd_dt_obj(dt)->oo_inode;
3326         LASSERT(inode);
3327
3328         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3329                                    true, true, NULL, false);
3330         RETURN(rc);
3331 }
3332
3333 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3334                                           struct dt_rec *fid)
3335 {
3336         struct osd_fid_pack *rec;
3337         int                  rc = -ENODATA;
3338
3339         if (de->file_type & LDISKFS_DIRENT_LUFID) {
3340                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3341                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3342         }
3343         return rc;
3344 }
3345
3346 static int osd_mdt_seq_exists(const struct lu_env *env,
3347                               struct osd_device *osd, obd_seq seq)
3348 {
3349         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
3350         struct seq_server_site  *ss = osd_seq_site(osd);
3351         int                     rc;
3352         ENTRY;
3353
3354         if (ss == NULL)
3355                 RETURN(1);
3356
3357         /* XXX: currently, each MDT only store avaible sequence on disk, and no
3358          * allocated sequences information on disk, so we have to lookup FLDB,
3359          * but it probably makes more sense also store allocated sequence
3360          * locally, so we do not need do remote FLDB lookup in OSD */
3361         rc = osd_fld_lookup(env, osd, seq, range);
3362         if (rc != 0) {
3363                 CERROR("%s: Can not lookup fld for "LPX64"\n",
3364                        osd_name(osd), seq);
3365                 RETURN(0);
3366         }
3367
3368         RETURN(ss->ss_node_id == range->lsr_index);
3369 }
3370
3371 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3372                           struct lu_fid *fid)
3373 {
3374         ENTRY;
3375
3376         /* FID seqs not in FLDB, must be local seq */
3377         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
3378                 RETURN(0);
3379
3380         /* Currently only check this for FID on MDT */
3381         if (osd_mdt_seq_exists(env, osd, fid_seq(fid)))
3382                 RETURN(0);
3383
3384         RETURN(1);
3385 }
3386
3387 /**
3388  * Index delete function for interoperability mode (b11826).
3389  * It will remove the directory entry added by osd_index_ea_insert().
3390  * This entry is needed to maintain name->fid mapping.
3391  *
3392  * \param key,  key i.e. file entry to be deleted
3393  *
3394  * \retval   0, on success
3395  * \retval -ve, on error
3396  */
3397 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3398                                const struct dt_key *key,
3399                                struct thandle *handle,
3400                                struct lustre_capa *capa)
3401 {
3402         struct osd_object          *obj    = osd_dt_obj(dt);
3403         struct inode               *dir    = obj->oo_inode;
3404         struct dentry              *dentry;
3405         struct osd_thandle         *oh;
3406         struct ldiskfs_dir_entry_2 *de = NULL;
3407         struct buffer_head         *bh;
3408         struct htree_lock          *hlock = NULL;
3409         struct lu_fid              *fid = &osd_oti_get(env)->oti_fid;
3410         struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
3411         int                        rc;
3412         ENTRY;
3413
3414         LINVRNT(osd_invariant(obj));
3415         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3416         LASSERT(handle != NULL);
3417
3418         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3419
3420         oh = container_of(handle, struct osd_thandle, ot_super);
3421         LASSERT(oh->ot_handle != NULL);
3422         LASSERT(oh->ot_handle->h_transaction != NULL);
3423
3424         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3425                 RETURN(-EACCES);
3426
3427         ll_vfs_dq_init(dir);
3428         dentry = osd_child_dentry_get(env, obj,
3429                                       (char *)key, strlen((char *)key));
3430
3431         if (obj->oo_hl_head != NULL) {
3432                 hlock = osd_oti_get(env)->oti_hlock;
3433                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3434                                    dir, LDISKFS_HLOCK_DEL);
3435         } else {
3436                 down_write(&obj->oo_ext_idx_sem);
3437         }
3438
3439         bh = ldiskfs_find_entry(dir, &dentry->d_name, &de, hlock);
3440         if (bh) {
3441                 __u32 ino = 0;
3442
3443                 /* If this is not the ".." entry, it might be a remote DNE
3444                  * entry and  we need to check if the FID is for a remote
3445                  * MDT.  If the FID is  not in the directory entry (e.g.
3446                  * upgraded 1.8 filesystem without dirdata enabled) then
3447                  * we need to get the FID from the LMA. For a remote directory
3448                  * there HAS to be an LMA, it cannot be an IGIF inode in this
3449                  * case.
3450                  *
3451                  * Delete the entry before the agent inode in order to
3452                  * simplify error handling.  At worst an error after deleting
3453                  * the entry first might leak the agent inode afterward. The
3454                  * reverse would need filesystem abort in case of error deleting
3455                  * the entry after the agent had been removed, or leave a
3456                  * dangling entry pointing at a random inode. */
3457                 if (strcmp((char *)key, dotdot) != 0) {
3458                         LASSERT(de != NULL);
3459                         rc = osd_get_fid_from_dentry(de, (struct dt_rec *)fid);
3460                         /* If Fid is not in dentry, try to get it from LMA */
3461                         if (rc == -ENODATA) {
3462                                 struct osd_inode_id *id;
3463                                 struct inode *inode;
3464
3465                                 /* Before trying to get fid from the inode,
3466                                  * check whether the inode is valid.
3467                                  *
3468                                  * If the inode has been deleted, do not go
3469                                  * ahead to do osd_ea_fid_get, which will set
3470                                  * the inode to bad inode, which might cause
3471                                  * the inode to be deleted uncorrectly */
3472                                 inode = ldiskfs_iget(osd_sb(osd),
3473                                                      le32_to_cpu(de->inode));
3474                                 if (IS_ERR(inode)) {
3475                                         CDEBUG(D_INODE, "%s: "DFID"get inode"
3476                                                "error.\n", osd_name(osd),
3477                                                PFID(fid));
3478                                         rc = PTR_ERR(inode);
3479                                 } else {
3480                                         if (likely(inode->i_nlink != 0)) {
3481                                                 id = &osd_oti_get(env)->oti_id;
3482                                                 rc = osd_ea_fid_get(env, obj,
3483                                                         le32_to_cpu(de->inode),
3484                                                                     fid, id);
3485                                         } else {
3486                                                 CDEBUG(D_INFO, "%s: %u "DFID
3487                                                        "deleted.\n",
3488                                                        osd_name(osd),
3489                                                        le32_to_cpu(de->inode),
3490                                                        PFID(fid));
3491                                                 rc = -ESTALE;
3492                                         }
3493                                         iput(inode);
3494                                 }
3495                         }
3496                         if (rc == 0 &&
3497                             unlikely(osd_remote_fid(env, osd, fid)))
3498                                 /* Need to delete agent inode */
3499                                 ino = le32_to_cpu(de->inode);
3500                 }
3501                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
3502                 brelse(bh);
3503                 if (rc == 0 && unlikely(ino != 0)) {
3504                         rc = osd_delete_local_agent_inode(env, osd, fid, ino,
3505                                                           oh);
3506                         if (rc != 0)
3507                                 CERROR("%s: del local inode "DFID": rc = %d\n",
3508                                        osd_name(osd), PFID(fid), rc);
3509                 }
3510         } else {
3511                 rc = -ENOENT;
3512         }
3513         if (hlock != NULL)
3514                 ldiskfs_htree_unlock(hlock);
3515         else
3516                 up_write(&obj->oo_ext_idx_sem);
3517
3518         if (rc != 0)
3519                 GOTO(out, rc);
3520
3521         /* For inode on the remote MDT, .. will point to
3522          * /Agent directory, Check whether it needs to delete
3523          * from agent directory */
3524         if (unlikely(strcmp((char *)key, dotdot) == 0)) {
3525                 rc = osd_delete_from_remote_parent(env, osd_obj2dev(obj), obj,
3526                                                    oh);
3527                 if (rc != 0 && rc != -ENOENT) {
3528                         CERROR("%s: delete agent inode "DFID": rc = %d\n",
3529                                osd_name(osd), PFID(fid), rc);
3530                 }
3531
3532                 if (rc == -ENOENT)
3533                         rc = 0;
3534
3535                 GOTO(out, rc);
3536         }
3537 out:
3538
3539         LASSERT(osd_invariant(obj));
3540         RETURN(rc);
3541 }
3542
3543 /**
3544  *      Lookup index for \a key and copy record to \a rec.
3545  *
3546  *      \param  dt      osd index object
3547  *      \param  key     key for index
3548  *      \param  rec     record reference
3549  *
3550  *      \retval  +ve  success : exact mach
3551  *      \retval  0    return record with key not greater than \a key
3552  *      \retval -ve   failure
3553  */
3554 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3555                                 struct dt_rec *rec, const struct dt_key *key,
3556                                 struct lustre_capa *capa)
3557 {
3558         struct osd_object      *obj = osd_dt_obj(dt);
3559         struct iam_path_descr  *ipd;
3560         struct iam_container   *bag = &obj->oo_dir->od_container;
3561         struct osd_thread_info *oti = osd_oti_get(env);
3562         struct iam_iterator    *it = &oti->oti_idx_it;
3563         struct iam_rec         *iam_rec;
3564         int                     rc;
3565
3566         ENTRY;
3567
3568         LASSERT(osd_invariant(obj));
3569         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3570         LASSERT(bag->ic_object == obj->oo_inode);
3571
3572         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3573                 RETURN(-EACCES);
3574
3575         ipd = osd_idx_ipd_get(env, bag);
3576         if (IS_ERR(ipd))
3577                 RETURN(-ENOMEM);
3578
3579         /* got ipd now we can start iterator. */
3580         iam_it_init(it, bag, 0, ipd);
3581
3582         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3583                 /* swab quota uid/gid provided by caller */
3584                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3585                 key = (const struct dt_key *)&oti->oti_quota_id;
3586         }
3587
3588         rc = iam_it_get(it, (struct iam_key *)key);
3589         if (rc >= 0) {
3590                 if (S_ISDIR(obj->oo_inode->i_mode))
3591                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3592                 else
3593                         iam_rec = (struct iam_rec *) rec;
3594
3595                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3596
3597                 if (S_ISDIR(obj->oo_inode->i_mode))
3598                         osd_fid_unpack((struct lu_fid *) rec,
3599                                        (struct osd_fid_pack *)iam_rec);
3600                 else if (fid_is_quota(lu_object_fid(&dt->do_lu)))
3601                         osd_quota_unpack(obj, rec);
3602         }
3603
3604         iam_it_put(it);
3605         iam_it_fini(it);
3606         osd_ipd_put(env, bag, ipd);
3607
3608         LINVRNT(osd_invariant(obj));
3609
3610         RETURN(rc);
3611 }
3612
3613 static int osd_index_declare_iam_insert(const struct lu_env *env,
3614                                         struct dt_object *dt,
3615                                         const struct dt_rec *rec,
3616                                         const struct dt_key *key,
3617                                         struct thandle *handle)
3618 {
3619         struct osd_thandle *oh;
3620
3621         LASSERT(handle != NULL);
3622
3623         oh = container_of0(handle, struct osd_thandle, ot_super);
3624         LASSERT(oh->ot_handle == NULL);
3625
3626         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
3627                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
3628
3629         return 0;
3630 }
3631
3632 /**
3633  *      Inserts (key, value) pair in \a dt index object.
3634  *
3635  *      \param  dt      osd index object
3636  *      \param  key     key for index
3637  *      \param  rec     record reference
3638  *      \param  th      transaction handler
3639  *
3640  *      \retval  0  success
3641  *      \retval -ve failure
3642  */
3643 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3644                                 const struct dt_rec *rec,
3645                                 const struct dt_key *key, struct thandle *th,
3646                                 struct lustre_capa *capa, int ignore_quota)
3647 {
3648         struct osd_object     *obj = osd_dt_obj(dt);
3649         struct iam_path_descr *ipd;
3650         struct osd_thandle    *oh;
3651         struct iam_container  *bag = &obj->oo_dir->od_container;
3652         struct osd_thread_info *oti = osd_oti_get(env);
3653         struct iam_rec         *iam_rec;
3654         int                     rc;
3655
3656         ENTRY;
3657
3658         LINVRNT(osd_invariant(obj));
3659         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
3660         LASSERT(bag->ic_object == obj->oo_inode);
3661         LASSERT(th != NULL);
3662
3663         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3664                 RETURN(-EACCES);
3665
3666         osd_trans_exec_op(env, th, OSD_OT_INSERT);
3667
3668         ipd = osd_idx_ipd_get(env, bag);
3669         if (unlikely(ipd == NULL))
3670                 RETURN(-ENOMEM);
3671
3672         oh = container_of0(th, struct osd_thandle, ot_super);
3673         LASSERT(oh->ot_handle != NULL);
3674         LASSERT(oh->ot_handle->h_transaction != NULL);
3675         if (S_ISDIR(obj->oo_inode->i_mode)) {
3676                 iam_rec = (struct iam_rec *)oti->oti_ldp;
3677                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3678         } else if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3679                 /* pack quota uid/gid */
3680                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3681                 key = (const struct dt_key *)&oti->oti_quota_id;
3682                 /* pack quota record */
3683                 rec = osd_quota_pack(obj, rec, &oti->oti_quota_rec);
3684                 iam_rec = (struct iam_rec *)rec;
3685         } else {
3686                 iam_rec = (struct iam_rec *)rec;
3687         }
3688
3689         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3690                         iam_rec, ipd);
3691         osd_ipd_put(env, bag, ipd);
3692         LINVRNT(osd_invariant(obj));
3693         RETURN(rc);
3694 }
3695
3696 /**
3697  * Calls ldiskfs_add_entry() to add directory entry
3698  * into the directory. This is required for
3699  * interoperability mode (b11826)
3700  *
3701  * \retval   0, on success
3702  * \retval -ve, on error
3703  */
3704 static int __osd_ea_add_rec(struct osd_thread_info *info,
3705                             struct osd_object *pobj, struct inode  *cinode,
3706                             const char *name, const struct dt_rec *fid,
3707                             struct htree_lock *hlock, struct thandle *th)
3708 {
3709         struct ldiskfs_dentry_param *ldp;
3710         struct dentry               *child;
3711         struct osd_thandle          *oth;
3712         int                          rc;
3713
3714         oth = container_of(th, struct osd_thandle, ot_super);
3715         LASSERT(oth->ot_handle != NULL);
3716         LASSERT(oth->ot_handle->h_transaction != NULL);
3717         LASSERT(pobj->oo_inode);
3718
3719         ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3720         if (unlikely(pobj->oo_inode ==
3721                      osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
3722                 ldp->edp_magic = 0;
3723         else
3724                 osd_get_ldiskfs_dirent_param(ldp, fid);
3725         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3726         child->d_fsdata = (void *)ldp;
3727         ll_vfs_dq_init(pobj->oo_inode);
3728         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3729
3730         RETURN(rc);
3731 }
3732
3733 /**
3734  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3735  * into the directory.Also sets flags into osd object to
3736  * indicate dot and dotdot are created. This is required for
3737  * interoperability mode (b11826)
3738  *
3739  * \param dir   directory for dot and dotdot fixup.
3740  * \param obj   child object for linking
3741  *
3742  * \retval   0, on success
3743  * \retval -ve, on error
3744  */
3745 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3746                               struct osd_object *dir,
3747                               struct inode  *parent_dir, const char *name,
3748                               const struct dt_rec *dot_fid,
3749                               const struct dt_rec *dot_dot_fid,
3750                               struct thandle *th)
3751 {
3752         struct inode                *inode = dir->oo_inode;
3753         struct osd_thandle          *oth;
3754         int result = 0;
3755
3756         oth = container_of(th, struct osd_thandle, ot_super);
3757         LASSERT(oth->ot_handle->h_transaction != NULL);
3758         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3759
3760         if (strcmp(name, dot) == 0) {
3761                 if (dir->oo_compat_dot_created) {
3762                         result = -EEXIST;
3763                 } else {
3764                         LASSERT(inode == parent_dir);
3765                         dir->oo_compat_dot_created = 1;
3766                         result = 0;
3767                 }
3768         } else if (strcmp(name, dotdot) == 0) {
3769                 if (!dir->oo_compat_dot_created)
3770                         return -EINVAL;
3771                 /* in case of rename, dotdot is already created */
3772                 if (dir->oo_compat_dotdot_created) {
3773                         return __osd_ea_add_rec(info, dir, parent_dir, name,
3774                                                 dot_dot_fid, NULL, th);
3775                 }
3776
3777                 result = osd_add_dot_dotdot_internal(info, dir->oo_inode,
3778                                                 parent_dir, dot_fid,
3779                                                 dot_dot_fid, oth);
3780                 if (result == 0)
3781                         dir->oo_compat_dotdot_created = 1;
3782         }
3783
3784         return result;
3785 }
3786
3787
3788 /**
3789  * It will call the appropriate osd_add* function and return the
3790  * value, return by respective functions.
3791  */
3792 static int osd_ea_add_rec(const struct lu_env *env, struct osd_object *pobj,
3793                           struct inode *cinode, const char *name,
3794                           const struct dt_rec *fid, struct thandle *th)
3795 {
3796         struct osd_thread_info *info   = osd_oti_get(env);
3797         struct htree_lock      *hlock;
3798         int                     rc;
3799
3800         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3801
3802         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3803                                                    name[2] =='\0'))) {
3804                 if (hlock != NULL) {
3805                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3806                                            pobj->oo_inode, 0);
3807                 } else {
3808                         down_write(&pobj->oo_ext_idx_sem);
3809                 }
3810                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3811                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3812                                         fid, th);
3813         } else {
3814                 if (hlock != NULL) {
3815                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3816                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
3817                 } else {
3818                         down_write(&pobj->oo_ext_idx_sem);
3819                 }
3820
3821                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_INDIR)) {
3822                         struct lu_fid *tfid = &info->oti_fid;
3823
3824                         *tfid = *(const struct lu_fid *)fid;
3825                         tfid->f_ver = ~0;
3826                         rc = __osd_ea_add_rec(info, pobj, cinode, name,
3827                                               (const struct dt_rec *)tfid,
3828                                               hlock, th);
3829                 } else {
3830                         rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3831                                               hlock, th);
3832                 }
3833         }
3834         if (hlock != NULL)
3835                 ldiskfs_htree_unlock(hlock);
3836         else
3837                 up_write(&pobj->oo_ext_idx_sem);
3838
3839         return rc;
3840 }
3841
3842 static void
3843 osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
3844                       struct osd_idmap_cache *oic)
3845 {
3846         struct osd_scrub    *scrub = &dev->od_scrub;
3847         struct lu_fid       *fid   = &oic->oic_fid;
3848         struct osd_inode_id *id    = &oti->oti_id;
3849         int                  once  = 0;
3850         int                  rc;
3851         ENTRY;
3852
3853         if (!fid_is_norm(fid) && !fid_is_igif(fid))
3854                 RETURN_EXIT;
3855
3856 again:
3857         rc = osd_oi_lookup(oti, dev, fid, id, OI_CHECK_FLD);
3858         if (rc != 0 && rc != -ENOENT)
3859                 RETURN_EXIT;
3860
3861         if (rc == 0 && osd_id_eq(id, &oic->oic_lid))
3862                 RETURN_EXIT;
3863
3864         if (thread_is_running(&scrub->os_thread)) {
3865                 rc = osd_oii_insert(dev, oic, rc == -ENOENT);
3866                 /* There is race condition between osd_oi_lookup and OI scrub.
3867                  * The OI scrub finished just after osd_oi_lookup() failure.
3868                  * Under such case, it is unnecessary to trigger OI scrub again,
3869                  * but try to call osd_oi_lookup() again. */
3870                 if (unlikely(rc == -EAGAIN))
3871                         goto again;
3872
3873                 RETURN_EXIT;
3874         }
3875
3876         if (!dev->od_noscrub && ++once == 1) {
3877                 CDEBUG(D_LFSCK, "Trigger OI scrub by RPC for "DFID"\n",
3878                        PFID(fid));
3879                 rc = osd_scrub_start(dev);
3880                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC for "DFID
3881                                ", rc = %d [2]\n",
3882                                LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
3883                                PFID(fid), rc);
3884                 if (rc == 0)
3885                         goto again;
3886         }
3887
3888         EXIT;
3889 }
3890
3891 static int osd_fail_fid_lookup(struct osd_thread_info *oti,
3892                                struct osd_device *dev,
3893                                struct osd_idmap_cache *oic,
3894                                struct lu_fid *fid, __u32 ino)
3895 {
3896         struct lustre_mdt_attrs *lma   = &oti->oti_mdt_attrs;
3897         struct inode            *inode;
3898         int                      rc;
3899
3900         osd_id_gen(&oic->oic_lid, ino, OSD_OII_NOGEN);
3901         inode = osd_iget(oti, dev, &oic->oic_lid);
3902         if (IS_ERR(inode)) {
3903                 fid_zero(&oic->oic_fid);
3904                 return PTR_ERR(inode);
3905         }
3906
3907         rc = osd_get_lma(oti, inode, &oti->oti_obj_dentry, lma);
3908         iput(inode);
3909         if (rc != 0)
3910                 fid_zero(&oic->oic_fid);
3911         else
3912                 *fid = oic->oic_fid = lma->lma_self_fid;
3913         return rc;
3914 }
3915
3916 int osd_add_oi_cache(struct osd_thread_info *info, struct osd_device *osd,
3917                      struct osd_inode_id *id, const struct lu_fid *fid)
3918 {
3919         CDEBUG(D_INODE, "add "DFID" %u:%u to info %p\n", PFID(fid),
3920                id->oii_ino, id->oii_gen, info);
3921         info->oti_cache.oic_lid = *id;
3922         info->oti_cache.oic_fid = *fid;
3923         info->oti_cache.oic_dev = osd;
3924
3925         return 0;
3926 }
3927
3928 /**
3929  * Calls ->lookup() to find dentry. From dentry get inode and
3930  * read inode's ea to get fid. This is required for  interoperability
3931  * mode (b11826)
3932  *
3933  * \retval   0, on success
3934  * \retval -ve, on error
3935  */
3936 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3937                              struct dt_rec *rec, const struct dt_key *key)
3938 {
3939         struct inode               *dir    = obj->oo_inode;
3940         struct dentry              *dentry;
3941         struct ldiskfs_dir_entry_2 *de;
3942         struct buffer_head         *bh;
3943         struct lu_fid              *fid = (struct lu_fid *) rec;
3944         struct htree_lock          *hlock = NULL;
3945         int                         ino;
3946         int                         rc;
3947         ENTRY;
3948
3949         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3950
3951         dentry = osd_child_dentry_get(env, obj,
3952                                       (char *)key, strlen((char *)key));
3953
3954         if (obj->oo_hl_head != NULL) {
3955                 hlock = osd_oti_get(env)->oti_hlock;
3956                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3957                                    dir, LDISKFS_HLOCK_LOOKUP);
3958         } else {
3959                 down_read(&obj->oo_ext_idx_sem);
3960         }
3961
3962         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3963         if (bh) {
3964                 struct osd_thread_info *oti = osd_oti_get(env);
3965                 struct osd_inode_id *id = &oti->oti_id;
3966                 struct osd_idmap_cache *oic = &oti->oti_cache;
3967                 struct osd_device *dev = osd_obj2dev(obj);
3968                 struct osd_scrub *scrub = &dev->od_scrub;
3969                 struct scrub_file *sf = &scrub->os_file;
3970
3971                 ino = le32_to_cpu(de->inode);
3972                 if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP)) {
3973                         brelse(bh);
3974                         rc = osd_fail_fid_lookup(oti, dev, oic, fid, ino);
3975                         GOTO(out, rc);
3976                 }
3977
3978                 rc = osd_get_fid_from_dentry(de, rec);
3979
3980                 /* done with de, release bh */
3981                 brelse(bh);
3982                 if (rc != 0)
3983                         rc = osd_ea_fid_get(env, obj, ino, fid, id);
3984                 else
3985                         osd_id_gen(id, ino, OSD_OII_NOGEN);
3986                 if (rc != 0) {
3987                         fid_zero(&oic->oic_fid);
3988                         GOTO(out, rc);
3989                 }
3990
3991                 rc = osd_add_oi_cache(osd_oti_get(env), osd_obj2dev(obj), id,
3992                                       fid);
3993                 if (rc != 0)
3994                         GOTO(out, rc);
3995                 if ((scrub->os_pos_current <= ino) &&
3996                     ((sf->sf_flags & SF_INCONSISTENT) ||
3997                      (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
3998                      ldiskfs_test_bit(osd_oi_fid2idx(dev, fid),
3999                                       sf->sf_oi_bitmap)))
4000                         osd_consistency_check(oti, dev, oic);
4001         } else {
4002                 rc = -ENOENT;
4003         }
4004
4005         GOTO(out, rc);
4006
4007 out:
4008         if (hlock != NULL)
4009                 ldiskfs_htree_unlock(hlock);
4010         else
4011                 up_read(&obj->oo_ext_idx_sem);
4012         return rc;
4013 }
4014
4015 /**
4016  * Find the osd object for given fid.
4017  *
4018  * \param fid need to find the osd object having this fid
4019  *
4020  * \retval osd_object on success
4021  * \retval        -ve on error
4022  */
4023 struct osd_object *osd_object_find(const struct lu_env *env,
4024                                    struct dt_object *dt,
4025                                    const struct lu_fid *fid)
4026 {
4027         struct lu_device  *ludev = dt->do_lu.lo_dev;
4028         struct osd_object *child = NULL;
4029         struct lu_object  *luch;
4030         struct lu_object  *lo;
4031
4032         /*
4033          * at this point topdev might not exist yet
4034          * (i.e. MGS is preparing profiles). so we can
4035          * not rely on topdev and instead lookup with
4036          * our device passed as topdev. this can't work
4037          * if the object isn't cached yet (as osd doesn't
4038          * allocate lu_header). IOW, the object must be
4039          * in the cache, otherwise lu_object_alloc() crashes
4040          * -bzzz
4041          */
4042         luch = lu_object_find_at(env, ludev, fid, NULL);
4043         if (!IS_ERR(luch)) {
4044                 if (lu_object_exists(luch)) {
4045                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
4046                         if (lo != NULL)
4047                                 child = osd_obj(lo);
4048                         else
4049                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
4050                                                 "lu_object can't be located"
4051                                                 DFID"\n", PFID(fid));
4052
4053                         if (child == NULL) {
4054                                 lu_object_put(env, luch);
4055                                 CERROR("Unable to get osd_object\n");
4056                                 child = ERR_PTR(-ENOENT);
4057                         }
4058                 } else {
4059                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
4060                                         "lu_object does not exists "DFID"\n",
4061                                         PFID(fid));
4062                         lu_object_put(env, luch);
4063                         child = ERR_PTR(-ENOENT);
4064                 }
4065         } else
4066                 child = (void *)luch;
4067
4068         return child;
4069 }
4070
4071 /**
4072  * Put the osd object once done with it.
4073  *
4074  * \param obj osd object that needs to be put
4075  */
4076 static inline void osd_object_put(const struct lu_env *env,
4077                                   struct osd_object *obj)
4078 {
4079         lu_object_put(env, &obj->oo_dt.do_lu);
4080 }
4081
4082 static int osd_index_declare_ea_insert(const struct lu_env *env,
4083                                        struct dt_object *dt,
4084                                        const struct dt_rec *rec,
4085                                        const struct dt_key *key,
4086                                        struct thandle *handle)
4087 {
4088         struct osd_thandle      *oh;
4089         struct osd_device       *osd   = osd_dev(dt->do_lu.lo_dev);
4090         struct lu_fid           *fid = (struct lu_fid *)rec;
4091         int                     rc;
4092         ENTRY;
4093
4094         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4095         LASSERT(handle != NULL);
4096
4097         oh = container_of0(handle, struct osd_thandle, ot_super);
4098         LASSERT(oh->ot_handle == NULL);
4099
4100         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4101                              osd_dto_credits_noquota[DTO_INDEX_INSERT]);
4102
4103         if (osd_dt_obj(dt)->oo_inode == NULL) {
4104                 const char *name  = (const char *)key;
4105                 /* Object is not being created yet. Only happens when
4106                  *     1. declare directory create
4107                  *     2. declare insert .
4108                  *     3. declare insert ..
4109                  */
4110                 LASSERT(strcmp(name, dotdot) == 0 || strcmp(name, dot) == 0);
4111         } else {
4112                 struct inode *inode = osd_dt_obj(dt)->oo_inode;
4113
4114                 /* We ignore block quota on meta pool (MDTs), so needn't
4115                  * calculate how many blocks will be consumed by this index
4116                  * insert */
4117                 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0,
4118                                            oh, true, true, NULL, false);
4119         }
4120
4121         if (fid == NULL)
4122                 RETURN(0);
4123
4124         rc = osd_remote_fid(env, osd, fid);
4125         if (rc <= 0)
4126                 RETURN(rc);
4127
4128         rc = 0;
4129
4130         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
4131                              osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
4132         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4133                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4134         osd_trans_declare_op(env, oh, OSD_OT_INSERT,
4135                              osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
4136
4137         RETURN(rc);
4138 }
4139
4140 /**
4141  * Index add function for interoperability mode (b11826).
4142  * It will add the directory entry.This entry is needed to
4143  * maintain name->fid mapping.
4144  *
4145  * \param key it is key i.e. file entry to be inserted
4146  * \param rec it is value of given key i.e. fid
4147  *
4148  * \retval   0, on success
4149  * \retval -ve, on error
4150  */
4151 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
4152                                const struct dt_rec *rec,
4153                                const struct dt_key *key, struct thandle *th,
4154                                struct lustre_capa *capa, int ignore_quota)
4155 {
4156         struct osd_object       *obj = osd_dt_obj(dt);
4157         struct osd_device       *osd = osd_dev(dt->do_lu.lo_dev);
4158         struct lu_fid           *fid = (struct lu_fid *) rec;
4159         const char              *name = (const char *)key;
4160         struct osd_thread_info  *oti   = osd_oti_get(env);
4161         struct osd_inode_id     *id    = &oti->oti_id;
4162         struct inode            *child_inode = NULL;
4163         struct osd_object       *child = NULL;
4164         int                     rc;
4165         ENTRY;
4166
4167         LASSERT(osd_invariant(obj));
4168         LASSERT(dt_object_exists(dt) && !dt_object_remote(dt));
4169         LASSERT(th != NULL);
4170
4171         osd_trans_exec_op(env, th, OSD_OT_INSERT);
4172
4173         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
4174                 RETURN(-EACCES);
4175
4176         LASSERTF(fid_is_sane(fid), "fid"DFID" is insane!", PFID(fid));
4177
4178         rc = osd_remote_fid(env, osd, fid);
4179         if (rc < 0) {
4180                 CERROR("%s: Can not find object "DFID" rc %d\n",
4181                        osd_name(osd), PFID(fid), rc);
4182                 RETURN(rc);
4183         }
4184
4185         if (rc == 1) {
4186                 /* Insert remote entry */
4187                 if (strcmp(name, dotdot) == 0 && strlen(name) == 2) {
4188                         struct osd_mdobj_map    *omm = osd->od_mdt_map;
4189                         struct osd_thandle      *oh;
4190
4191                         /* If parent on remote MDT, we need put this object
4192                          * under AGENT */
4193                         oh = container_of(th, typeof(*oh), ot_super);
4194                         rc = osd_add_to_remote_parent(env, osd, obj, oh);
4195                         if (rc != 0) {
4196                                 CERROR("%s: add "DFID" error: rc = %d\n",
4197                                        osd_name(osd),
4198                                        PFID(lu_object_fid(&dt->do_lu)), rc);
4199                                 RETURN(rc);
4200                         }
4201
4202                         child_inode = igrab(omm->omm_remote_parent->d_inode);
4203                 } else {
4204                         child_inode = osd_create_local_agent_inode(env, osd,
4205                                                                    obj, fid,
4206                                                                    th);
4207                         if (IS_ERR(child_inode))
4208                                 RETURN(PTR_ERR(child_inode));
4209                 }
4210         } else {
4211                 /* Insert local entry */
4212                 child = osd_object_find(env, dt, fid);
4213                 if (IS_ERR(child)) {
4214                         CERROR("%s: Can not find object "DFID"%u:%u: rc = %d\n",
4215                                osd_name(osd), PFID(fid),
4216                                id->oii_ino, id->oii_gen,
4217                                (int)PTR_ERR(child_inode));
4218                         RETURN(PTR_ERR(child_inode));
4219                 }
4220                 child_inode = igrab(child->oo_inode);
4221         }
4222
4223         rc = osd_ea_add_rec(env, obj, child_inode, name, rec, th);
4224
4225         iput(child_inode);
4226         if (child != NULL)
4227                 osd_object_put(env, child);
4228         LASSERT(osd_invariant(obj));
4229         RETURN(rc);
4230 }
4231
4232 /**
4233  *  Initialize osd Iterator for given osd index object.
4234  *
4235  *  \param  dt      osd index object
4236  */
4237
4238 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
4239                                      struct dt_object *dt,
4240                                      __u32 unused,
4241                                      struct lustre_capa *capa)
4242 {
4243         struct osd_it_iam      *it;
4244         struct osd_thread_info *oti = osd_oti_get(env);
4245         struct osd_object      *obj = osd_dt_obj(dt);
4246         struct lu_object       *lo  = &dt->do_lu;
4247         struct iam_path_descr  *ipd;
4248         struct iam_container   *bag = &obj->oo_dir->od_container;
4249
4250         LASSERT(lu_object_exists(lo));
4251
4252         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
4253                 return ERR_PTR(-EACCES);
4254
4255         it = &oti->oti_it;
4256         ipd = osd_it_ipd_get(env, bag);
4257         if (likely(ipd != NULL)) {
4258                 it->oi_obj = obj;
4259                 it->oi_ipd = ipd;
4260                 lu_object_get(lo);
4261                 iam_it_init(&it->oi_it, bag, IAM_IT_MOVE, ipd);
4262                 return (struct dt_it *)it;
4263         }
4264         return ERR_PTR(-ENOMEM);
4265 }
4266
4267 /**
4268  * free given Iterator.
4269  */
4270
4271 static void osd_it_iam_fini(const struct lu_env *env, struct dt_it *di)
4272 {
4273         struct osd_it_iam *it = (struct osd_it_iam *)di;
4274         struct osd_object *obj = it->oi_obj;
4275
4276         iam_it_fini(&it->oi_it);
4277         osd_ipd_put(env, &obj->oo_dir->od_container, it->oi_ipd);
4278         lu_object_put(env, &obj->oo_dt.do_lu);
4279 }
4280
4281 /**
4282  *  Move Iterator to record specified by \a key
4283  *
4284  *  \param  di      osd iterator
4285  *  \param  key     key for index
4286  *
4287  *  \retval +ve  di points to record with least key not larger than key
4288  *  \retval  0   di points to exact matched key
4289  *  \retval -ve  failure
4290  */
4291
4292 static int osd_it_iam_get(const struct lu_env *env,
4293                           struct dt_it *di, const struct dt_key *key)
4294 {
4295         struct osd_thread_info  *oti = osd_oti_get(env);
4296         struct osd_it_iam       *it = (struct osd_it_iam *)di;
4297
4298         if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4299                 /* swab quota uid/gid */
4300                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
4301                 key = (struct dt_key *)&oti->oti_quota_id;
4302         }
4303
4304         return iam_it_get(&it->oi_it, (const struct iam_key *)key);
4305 }
4306
4307 /**
4308  *  Release Iterator
4309  *
4310  *  \param  di      osd iterator
4311  */
4312 static void osd_it_iam_put(const struct lu_env *env, struct dt_it *di)
4313 {
4314         struct osd_it_iam *it = (struct osd_it_iam *)di;
4315
4316         iam_it_put(&it->oi_it);
4317 }
4318
4319 /**
4320  *  Move iterator by one record
4321  *
4322  *  \param  di      osd iterator
4323  *
4324  *  \retval +1   end of container reached
4325  *  \retval  0   success
4326  *  \retval -ve  failure
4327  */
4328
4329 static int osd_it_iam_next(const struct lu_env *env, struct dt_it *di)
4330 {
4331         struct osd_it_iam *it = (struct osd_it_iam *)di;
4332
4333         return iam_it_next(&it->oi_it);
4334 }
4335
4336 /**
4337  * Return pointer to the key under iterator.
4338  */
4339
4340 static struct dt_key *osd_it_iam_key(const struct lu_env *env,
4341                                  const struct dt_it *di)
4342 {
4343         struct osd_thread_info *oti = osd_oti_get(env);
4344         struct osd_it_iam      *it = (struct osd_it_iam *)di;
4345         struct osd_object      *obj = it->oi_obj;
4346         struct dt_key          *key;
4347
4348         key = (struct dt_key *)iam_it_key_get(&it->oi_it);
4349
4350         if (!IS_ERR(key) && fid_is_quota(lu_object_fid(&obj->oo_dt.do_lu))) {
4351                 /* swab quota uid/gid */
4352                 oti->oti_quota_id = le64_to_cpu(*((__u64 *)key));
4353                 key = (struct dt_key *)&oti->oti_quota_id;
4354         }
4355
4356         return key;
4357 }
4358
4359 /**
4360  * Return size of key under iterator (in bytes)
4361  */
4362
4363 static int osd_it_iam_key_size(const struct lu_env *env, const struct dt_it *di)
4364 {
4365         struct osd_it_iam *it = (struct osd_it_iam *)di;
4366
4367         return iam_it_key_size(&it->oi_it);
4368 }
4369
4370 static inline void
4371 osd_it_append_attrs(struct lu_dirent *ent, int len, __u16 type)
4372 {
4373         /* check if file type is required */
4374         if (ent->lde_attrs & LUDA_TYPE) {
4375                 struct luda_type *lt;
4376                 int align = sizeof(*lt) - 1;
4377
4378                 len = (len + align) & ~align;
4379                 lt = (struct luda_type *)(ent->lde_name + len);
4380                 lt->lt_type = cpu_to_le16(DTTOIF(type));
4381         }
4382
4383         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
4384 }
4385
4386 /**
4387  * build lu direct from backend fs dirent.
4388  */
4389
4390 static inline void
4391 osd_it_pack_dirent(struct lu_dirent *ent, struct lu_fid *fid, __u64 offset,
4392                    char *name, __u16 namelen, __u16 type, __u32 attr)
4393 {
4394         ent->lde_attrs = attr | LUDA_FID;
4395         fid_cpu_to_le(&ent->lde_fid, fid);
4396
4397         ent->lde_hash = cpu_to_le64(offset);
4398         ent->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
4399
4400         strncpy(ent->lde_name, name, namelen);
4401         ent->lde_name[namelen] = '\0';
4402         ent->lde_namelen = cpu_to_le16(namelen);
4403
4404         /* append lustre attributes */
4405         osd_it_append_attrs(ent, namelen, type);
4406 }
4407
4408 /**
4409  * Return pointer to the record under iterator.
4410  */
4411 static int osd_it_iam_rec(const struct lu_env *env,
4412                           const struct dt_it *di,
4413                           struct dt_rec *dtrec, __u32 attr)
4414 {
4415         struct osd_it_iam      *it   = (struct osd_it_iam *)di;
4416         struct osd_thread_info *info = osd_oti_get(env);
4417         ENTRY;
4418
4419         if (S_ISDIR(it->oi_obj->oo_inode->i_mode)) {
4420                 const struct osd_fid_pack *rec;
4421                 struct lu_fid             *fid = &info->oti_fid;
4422                 struct lu_dirent          *lde = (struct lu_dirent *)dtrec;
4423                 char                      *name;
4424                 int                        namelen;
4425                 __u64                      hash;
4426                 int                        rc;
4427
4428                 name = (char *)iam_it_key_get(&it->oi_it);
4429                 if (IS_ERR(name))
4430                         RETURN(PTR_ERR(name));
4431
4432                 namelen = iam_it_key_size(&it->oi_it);
4433
4434                 rec = (const struct osd_fid_pack *)iam_it_rec_get(&it->oi_it);
4435                 if (IS_ERR(rec))
4436                         RETURN(PTR_ERR(rec));
4437
4438                 rc = osd_fid_unpack(fid, rec);
4439                 if (rc)
4440                         RETURN(rc);
4441
4442                 hash = iam_it_store(&it->oi_it);
4443
4444                 /* IAM does not store object type in IAM index (dir) */
4445                 osd_it_pack_dirent(lde, fid, hash, name, namelen,
4446                                    0, LUDA_FID);
4447         } else if (fid_is_quota(lu_object_fid(&it->oi_obj->oo_dt.do_lu))) {
4448                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4449                            (struct iam_rec *)dtrec);
4450                 osd_quota_unpack(it->oi_obj, dtrec);
4451         } else {
4452                 iam_reccpy(&it->oi_it.ii_path.ip_leaf,
4453                            (struct iam_rec *)dtrec);
4454         }
4455
4456         RETURN(0);
4457 }
4458
4459 /**
4460  * Returns cookie for current Iterator position.
4461  */
4462 static __u64 osd_it_iam_store(const struct lu_env *env, const struct dt_it *di)
4463 {
4464         struct osd_it_iam *it = (struct osd_it_iam *)di;
4465
4466         return iam_it_store(&it->oi_it);
4467 }
4468
4469 /**
4470  * Restore iterator from cookie.
4471  *
4472  * \param  di      osd iterator
4473  * \param  hash    Iterator location cookie
4474  *
4475  * \retval +ve  di points to record with least key not larger than key.
4476  * \retval  0   di points to exact matched key
4477  * \retval -ve  failure
4478  */
4479
4480 static int osd_it_iam_load(const struct lu_env *env,
4481                            const struct dt_it *di, __u64 hash)
4482 {
4483         struct osd_it_iam *it = (struct osd_it_iam *)di;
4484
4485         return iam_it_load(&it->oi_it, hash);
4486 }
4487
4488 static const struct dt_index_operations osd_index_iam_ops = {
4489         .dio_lookup         = osd_index_iam_lookup,
4490         .dio_declare_insert = osd_index_declare_iam_insert,
4491         .dio_insert         = osd_index_iam_insert,
4492         .dio_declare_delete = osd_index_declare_iam_delete,
4493         .dio_delete         = osd_index_iam_delete,
4494         .dio_it     = {
4495                 .init     = osd_it_iam_init,
4496                 .fini     = osd_it_iam_fini,
4497                 .get      = osd_it_iam_get,
4498                 .put      = osd_it_iam_put,
4499                 .next     = osd_it_iam_next,
4500                 .key      = osd_it_iam_key,
4501                 .key_size = osd_it_iam_key_size,
4502                 .rec      = osd_it_iam_rec,
4503                 .store    = osd_it_iam_store,
4504                 .load     = osd_it_iam_load
4505         }
4506 };
4507
4508
4509 /**
4510  * Creates or initializes iterator context.
4511  *
4512  * \retval struct osd_it_ea, iterator structure on success
4513  *
4514  */
4515 static struct dt_it *osd_it_ea_init(const struct lu_env *env,
4516                                     struct dt_object *dt,
4517                                     __u32 attr,
4518                                     struct lustre_capa *capa)
4519 {
4520         struct osd_object       *obj  = osd_dt_obj(dt);
4521         struct osd_thread_info  *info = osd_oti_get(env);
4522         struct osd_it_ea        *it   = &info->oti_it_ea;
4523         struct file             *file = &it->oie_file;
4524         struct lu_object        *lo   = &dt->do_lu;
4525         struct dentry           *obj_dentry = &info->oti_it_dentry;
4526         ENTRY;
4527         LASSERT(lu_object_exists(lo));
4528
4529         obj_dentry->d_inode = obj->oo_inode;
4530         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
4531         obj_dentry->d_name.hash = 0;
4532
4533         it->oie_rd_dirent       = 0;
4534         it->oie_it_dirent       = 0;
4535         it->oie_dirent          = NULL;
4536         it->oie_buf             = info->oti_it_ea_buf;
4537         it->oie_obj             = obj;
4538
4539         /* Reset the "file" totally to avoid to reuse any old value from
4540          * former readdir handling, the "file->f_pos" should be zero. */
4541         memset(file, 0, sizeof(*file));
4542         /* Only FMODE_64BITHASH or FMODE_32BITHASH should be set, NOT both. */
4543         if (attr & LUDA_64BITHASH)
4544                 file->f_mode    = FMODE_64BITHASH;
4545         else
4546                 file->f_mode    = FMODE_32BITHASH;
4547         file->f_dentry          = obj_dentry;
4548         file->f_mapping         = obj->oo_inode->i_mapping;
4549         file->f_op              = obj->oo_inode->i_fop;
4550         lu_object_get(lo);
4551         RETURN((struct dt_it *) it);
4552 }
4553
4554 /**
4555  * Destroy or finishes iterator context.
4556  *
4557  * \param di iterator structure to be destroyed
4558  */
4559 static void osd_it_ea_fini(const struct lu_env *env, struct dt_it *di)
4560 {
4561         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
4562         struct osd_object    *obj  = it->oie_obj;
4563         struct inode       *inode  = obj->oo_inode;
4564
4565         ENTRY;
4566         it->oie_file.f_op->release(inode, &it->oie_file);
4567         lu_object_put(env, &obj->oo_dt.do_lu);
4568         EXIT;
4569 }
4570
4571 /**
4572  * It position the iterator at given key, so that next lookup continues from
4573  * that key Or it is similar to dio_it->load() but based on a key,
4574  * rather than file position.
4575  *
4576  * As a special convention, osd_it_ea_get(env, di, "") has to rewind iterator
4577  * to the beginning.
4578  *
4579  * TODO: Presently return +1 considering it is only used by mdd_dir_is_empty().
4580  */
4581 static int osd_it_ea_get(const struct lu_env *env,
4582                          struct dt_it *di, const struct dt_key *key)
4583 {
4584         struct osd_it_ea     *it   = (struct osd_it_ea *)di;
4585
4586         ENTRY;
4587         LASSERT(((const char *)key)[0] == '\0');
4588         it->oie_file.f_pos      = 0;
4589         it->oie_rd_dirent       = 0;
4590         it->oie_it_dirent       = 0;
4591         it->oie_dirent          = NULL;
4592
4593         RETURN(+1);
4594 }
4595
4596 /**
4597  * Does nothing
4598  */
4599 static void osd_it_ea_put(const struct lu_env *env, struct dt_it *di)
4600 {
4601 }
4602
4603 /**
4604  * It is called internally by ->readdir(). It fills the
4605  * iterator's in-memory data structure with required
4606  * information i.e. name, namelen, rec_size etc.
4607  *
4608  * \param buf in which information to be filled in.
4609  * \param name name of the file in given dir
4610  *
4611  * \retval 0 on success
4612  * \retval 1 on buffer full
4613  */
4614 static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
4615                                loff_t offset, __u64 ino,
4616                                unsigned d_type)
4617 {
4618         struct osd_it_ea        *it   = (struct osd_it_ea *)buf;
4619         struct osd_object       *obj  = it->oie_obj;
4620         struct osd_it_ea_dirent *ent  = it->oie_dirent;
4621         struct lu_fid           *fid  = &ent->oied_fid;
4622         struct osd_fid_pack     *rec;
4623         ENTRY;
4624
4625         /* this should never happen */
4626         if (unlikely(namelen == 0 || namelen > LDISKFS_NAME_LEN)) {
4627                 CERROR("ldiskfs return invalid namelen %d\n", namelen);
4628                 RETURN(-EIO);
4629         }
4630
4631         if ((void *) ent - it->oie_buf + sizeof(*ent) + namelen >
4632             OSD_IT_EA_BUFSIZE)
4633                 RETURN(1);
4634
4635         /* "." is just the object itself. */
4636         if (namelen == 1 && name[0] == '.') {
4637                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4638         } else if (d_type & LDISKFS_DIRENT_LUFID) {
4639                 rec = (struct osd_fid_pack*) (name + namelen + 1);
4640                 if (osd_fid_unpack(fid, rec) != 0)
4641                         fid_zero(fid);
4642         } else {
4643                 fid_zero(fid);
4644         }
4645         d_type &= ~LDISKFS_DIRENT_LUFID;
4646
4647         /* NOT export local root. */
4648         if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
4649                 ino = obj->oo_inode->i_ino;
4650                 *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
4651         }
4652
4653         ent->oied_ino     = ino;
4654         ent->oied_off     = offset;
4655         ent->oied_namelen = namelen;
4656         ent->oied_type    = d_type;
4657
4658         memcpy(ent->oied_name, name, namelen);
4659
4660         it->oie_rd_dirent++;
4661         it->oie_dirent = (void *) ent + cfs_size_round(sizeof(*ent) + namelen);
4662         RETURN(0);
4663 }
4664
4665 /**
4666  * Calls ->readdir() to load a directory entry at a time
4667  * and stored it in iterator's in-memory data structure.
4668  *
4669  * \param di iterator's in memory structure
4670  *
4671  * \retval   0 on success
4672  * \retval -ve on error
4673  */
4674 static int osd_ldiskfs_it_fill(const struct lu_env *env,
4675                                const struct dt_it *di)
4676 {
4677         struct osd_it_ea   *it    = (struct osd_it_ea *)di;
4678         struct osd_object  *obj   = it->oie_obj;
4679         struct inode       *inode = obj->oo_inode;
4680         struct htree_lock  *hlock = NULL;
4681         int                 result = 0;
4682
4683         ENTRY;
4684         it->oie_dirent = it->oie_buf;
4685         it->oie_rd_dirent = 0;
4686
4687         if (obj->oo_hl_head != NULL) {
4688                 hlock = osd_oti_get(env)->oti_hlock;
4689                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
4690                                    inode, LDISKFS_HLOCK_READDIR);
4691         } else {
4692                 down_read(&obj->oo_ext_idx_sem);
4693         }
4694
4695         result = inode->i_fop->readdir(&it->oie_file, it,
4696                                        (filldir_t) osd_ldiskfs_filldir);
4697
4698         if (hlock != NULL)
4699                 ldiskfs_htree_unlock(hlock);
4700         else
4701                 up_read(&obj->oo_ext_idx_sem);
4702
4703         if (it->oie_rd_dirent == 0) {
4704                 result = -EIO;
4705         } else {
4706                 it->oie_dirent = it->oie_buf;
4707                 it->oie_it_dirent = 1;
4708         }
4709
4710         RETURN(result);
4711 }
4712
4713 /**
4714  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
4715  * to load a directory entry at a time and stored it in
4716  * iterator's in-memory data structure.
4717  *
4718  * \param di iterator's in memory structure
4719  *
4720  * \retval +ve iterator reached to end
4721  * \retval   0 iterator not reached to end
4722  * \retval -ve on error
4723  */
4724 static int osd_it_ea_next(const struct lu_env *env, struct dt_it *di)
4725 {
4726         struct osd_it_ea *it = (struct osd_it_ea *)di;
4727         int rc;
4728
4729         ENTRY;
4730
4731         if (it->oie_it_dirent < it->oie_rd_dirent) {
4732                 it->oie_dirent =
4733                         (void *) it->oie_dirent +
4734                         cfs_size_round(sizeof(struct osd_it_ea_dirent) +
4735                                        it->oie_dirent->oied_namelen);
4736                 it->oie_it_dirent++;
4737                 RETURN(0);
4738         } else {
4739                 if (it->oie_file.f_pos == ldiskfs_get_htree_eof(&it->oie_file))
4740                         rc = +1;
4741                 else
4742                         rc = osd_ldiskfs_it_fill(env, di);
4743         }
4744
4745         RETURN(rc);
4746 }
4747
4748 /**
4749  * Returns the key at current position from iterator's in memory structure.
4750  *
4751  * \param di iterator's in memory structure
4752  *
4753  * \retval key i.e. struct dt_key on success
4754  */
4755 static struct dt_key *osd_it_ea_key(const struct lu_env *env,
4756                                     const struct dt_it *di)
4757 {
4758         struct osd_it_ea *it = (struct osd_it_ea *)di;
4759
4760         return (struct dt_key *)it->oie_dirent->oied_name;
4761 }
4762
4763 /**
4764  * Returns the key's size at current position from iterator's in memory structure.
4765  *
4766  * \param di iterator's in memory structure
4767  *
4768  * \retval key_size i.e. struct dt_key on success
4769  */
4770 static int osd_it_ea_key_size(const struct lu_env *env, const struct dt_it *di)
4771 {
4772         struct osd_it_ea *it = (struct osd_it_ea *)di;
4773
4774         return it->oie_dirent->oied_namelen;
4775 }
4776
4777 static int
4778 osd_dirent_update(handle_t *jh, struct super_block *sb,
4779                   struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4780                   struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de)
4781 {
4782         struct osd_fid_pack *rec;
4783         int                  rc;
4784         ENTRY;
4785
4786         LASSERT(de->file_type & LDISKFS_DIRENT_LUFID);
4787         LASSERT(de->rec_len >= de->name_len + sizeof(struct osd_fid_pack));
4788
4789         rc = ldiskfs_journal_get_write_access(jh, bh);
4790         if (rc != 0) {
4791                 CERROR("%.16s: fail to write access for update dirent: "
4792                        "name = %.*s, rc = %d\n",
4793                        LDISKFS_SB(sb)->s_es->s_volume_name,
4794                        ent->oied_namelen, ent->oied_name, rc);
4795                 RETURN(rc);
4796         }
4797
4798         rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4799         fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4800         rc = ldiskfs_journal_dirty_metadata(jh, bh);
4801         if (rc != 0)
4802                 CERROR("%.16s: fail to dirty metadata for update dirent: "
4803                        "name = %.*s, rc = %d\n",
4804                        LDISKFS_SB(sb)->s_es->s_volume_name,
4805                        ent->oied_namelen, ent->oied_name, rc);
4806
4807         RETURN(rc);
4808 }
4809
4810 static inline int
4811 osd_dirent_has_space(__u16 reclen, __u16 namelen, unsigned blocksize)
4812 {
4813         if (ldiskfs_rec_len_from_disk(reclen, blocksize) >=
4814             __LDISKFS_DIR_REC_LEN(namelen + 1 + sizeof(struct osd_fid_pack)))
4815                 return 1;
4816         else
4817                 return 0;
4818 }
4819
4820 static inline int
4821 osd_dot_dotdot_has_space(struct ldiskfs_dir_entry_2 *de, int dot_dotdot)
4822 {
4823         LASSERTF(dot_dotdot == 1 || dot_dotdot == 2,
4824                  "dot_dotdot = %d\n", dot_dotdot);
4825
4826         if (LDISKFS_DIR_REC_LEN(de) >=
4827             __LDISKFS_DIR_REC_LEN(dot_dotdot + 1 + sizeof(struct osd_fid_pack)))
4828                 return 1;
4829         else
4830                 return 0;
4831 }
4832
4833 static int
4834 osd_dirent_reinsert(const struct lu_env *env, handle_t *jh,
4835                     struct inode *dir, struct inode *inode,
4836                     struct osd_it_ea_dirent *ent, struct lu_fid *fid,
4837                     struct buffer_head *bh, struct ldiskfs_dir_entry_2 *de,
4838                     struct htree_lock *hlock)
4839 {
4840         struct dentry               *dentry;
4841         struct osd_fid_pack         *rec;
4842         struct ldiskfs_dentry_param *ldp;
4843         int                          rc;
4844         ENTRY;
4845
4846         if (!LDISKFS_HAS_INCOMPAT_FEATURE(inode->i_sb,
4847                                           LDISKFS_FEATURE_INCOMPAT_DIRDATA))
4848                 RETURN(0);
4849
4850         /* There is enough space to hold the FID-in-dirent. */
4851         if (osd_dirent_has_space(de->rec_len, ent->oied_namelen,
4852                                  dir->i_sb->s_blocksize)) {
4853                 rc = ldiskfs_journal_get_write_access(jh, bh);
4854                 if (rc != 0) {
4855                         CERROR("%.16s: fail to write access for reinsert "
4856                                "dirent: name = %.*s, rc = %d\n",
4857                                LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4858                                ent->oied_namelen, ent->oied_name, rc);
4859                         RETURN(rc);
4860                 }
4861
4862                 de->name[de->name_len] = 0;
4863                 rec = (struct osd_fid_pack *)(de->name + de->name_len + 1);
4864                 rec->fp_len = sizeof(struct lu_fid) + 1;
4865                 fid_cpu_to_be((struct lu_fid *)rec->fp_area, fid);
4866                 de->file_type |= LDISKFS_DIRENT_LUFID;
4867
4868                 rc = ldiskfs_journal_dirty_metadata(jh, bh);
4869                 if (rc != 0)
4870                         CERROR("%.16s: fail to dirty metadata for reinsert "
4871                                "dirent: name = %.*s, rc = %d\n",
4872                                LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4873                                ent->oied_namelen, ent->oied_name, rc);
4874
4875                 RETURN(rc);
4876         }
4877
4878         rc = ldiskfs_delete_entry(jh, dir, de, bh);
4879         if (rc != 0) {
4880                 CERROR("%.16s: fail to delete entry for reinsert dirent: "
4881                        "name = %.*s, rc = %d\n",
4882                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4883                        ent->oied_namelen, ent->oied_name, rc);
4884                 RETURN(rc);
4885         }
4886
4887         dentry = osd_child_dentry_by_inode(env, dir, ent->oied_name,
4888                                            ent->oied_namelen);
4889         ldp = (struct ldiskfs_dentry_param *)osd_oti_get(env)->oti_ldp;
4890         osd_get_ldiskfs_dirent_param(ldp, (const struct dt_rec *)fid);
4891         dentry->d_fsdata = (void *)ldp;
4892         ll_vfs_dq_init(dir);
4893         rc = osd_ldiskfs_add_entry(jh, dentry, inode, hlock);
4894         /* It is too bad, we cannot reinsert the name entry back.
4895          * That means we lose it! */
4896         if (rc != 0)
4897                 CERROR("%.16s: fail to insert entry for reinsert dirent: "
4898                        "name = %.*s, rc = %d\n",
4899                        LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
4900                        ent->oied_namelen, ent->oied_name, rc);
4901
4902         RETURN(rc);
4903 }
4904
4905 static int
4906 osd_dirent_check_repair(const struct lu_env *env, struct osd_object *obj,
4907                         struct osd_it_ea *it, struct lu_fid *fid,
4908                         struct osd_inode_id *id, __u32 *attr)
4909 {
4910         struct osd_thread_info     *info        = osd_oti_get(env);
4911         struct lustre_mdt_attrs    *lma         = &info->oti_mdt_attrs;
4912         struct osd_device          *dev         = osd_obj2dev(obj);
4913         struct super_block         *sb          = osd_sb(dev);
4914         const char                 *devname     =
4915                                         LDISKFS_SB(sb)->s_es->s_volume_name;
4916         struct osd_it_ea_dirent    *ent         = it->oie_dirent;
4917         struct inode               *dir         = obj->oo_inode;
4918         struct htree_lock          *hlock       = NULL;
4919         struct buffer_head         *bh          = NULL;
4920         handle_t                   *jh          = NULL;
4921         struct ldiskfs_dir_entry_2 *de;
4922         struct dentry              *dentry;
4923         struct inode               *inode;
4924         int                         credits;
4925         int                         rc;
4926         int                         dot_dotdot  = 0;
4927         bool                        dirty       = false;
4928         ENTRY;
4929
4930         if (ent->oied_name[0] == '.') {
4931                 if (ent->oied_namelen == 1)
4932                         dot_dotdot = 1;
4933                 else if (ent->oied_namelen == 2 && ent->oied_name[1] == '.')
4934                         dot_dotdot = 2;
4935         }
4936
4937         dentry = osd_child_dentry_get(env, obj, ent->oied_name,
4938                                       ent->oied_namelen);
4939
4940         /* We need to ensure that the name entry is still valid.
4941          * Because it may be removed or renamed by other already.
4942          *
4943          * The unlink or rename operation will start journal before PDO lock,
4944          * so to avoid deadlock, here we need to start journal handle before
4945          * related PDO lock also. But because we do not know whether there
4946          * will be something to be repaired before PDO lock, we just start
4947          * journal without conditions.
4948          *
4949          * We may need to remove the name entry firstly, then insert back.
4950          * One credit is for user quota file update.
4951          * One credit is for group quota file update.
4952          * Two credits are for dirty inode. */
4953         credits = osd_dto_credits_noquota[DTO_INDEX_DELETE] +
4954                   osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1 + 1 + 2;
4955
4956 again:
4957         if (dev->od_dirent_journal) {
4958                 jh = ldiskfs_journal_start_sb(sb, credits);
4959                 if (IS_ERR(jh)) {
4960                         rc = PTR_ERR(jh);
4961                         CERROR("%.16s: fail to start trans for dirent "
4962                                "check_repair: credits %d, name %.*s, rc %d\n",
4963                                devname, credits, ent->oied_namelen,
4964                                ent->oied_name, rc);
4965                         RETURN(rc);
4966                 }
4967
4968                 if (obj->oo_hl_head != NULL) {
4969                         hlock = osd_oti_get(env)->oti_hlock;
4970                         /* "0" means exclusive lock for the whole directory.
4971                          * We need to prevent others access such name entry
4972                          * during the delete + insert. Neither HLOCK_ADD nor
4973                          * HLOCK_DEL cannot guarantee the atomicity. */
4974                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir, 0);
4975                 } else {
4976                         down_write(&obj->oo_ext_idx_sem);
4977                 }
4978         } else {
4979                 if (obj->oo_hl_head != NULL) {
4980                         hlock = osd_oti_get(env)->oti_hlock;
4981                         ldiskfs_htree_lock(hlock, obj->oo_hl_head, dir,
4982                                            LDISKFS_HLOCK_LOOKUP);
4983                 } else {
4984                         down_read(&obj->oo_ext_idx_sem);
4985                 }
4986         }
4987
4988         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
4989         /* For dot/dotdot entry, if there is not enough space to hold the
4990          * FID-in-dirent, just keep them there. It only happens when the
4991          * device upgraded from 1.8 or restored from MDT file-level backup.
4992          * For the whole directory, only dot/dotdot entry have no FID-in-dirent
4993          * and needs to get FID from LMA when readdir, it will not affect the
4994          * performance much. */
4995         if ((bh == NULL) || (le32_to_cpu(de->inode) != ent->oied_ino) ||
4996             (dot_dotdot != 0 && !osd_dot_dotdot_has_space(de, dot_dotdot))) {
4997                 *attr |= LUDA_IGNORE;
4998                 GOTO(out_journal, rc = 0);
4999         }
5000
5001         osd_id_gen(id, ent->oied_ino, OSD_OII_NOGEN);
5002         inode = osd_iget(info, dev, id);
5003         if (IS_ERR(inode)) {
5004                 rc = PTR_ERR(inode);
5005                 if (rc == -ENOENT || rc == -ESTALE) {
5006                         *attr |= LUDA_IGNORE;
5007                         rc = 0;
5008                 }
5009
5010                 GOTO(out_journal, rc);
5011         }
5012
5013         /* skip the REMOTE_PARENT_DIR. */
5014         if (inode == dev->od_mdt_map->omm_remote_parent->d_inode)
5015                 GOTO(out_inode, rc = 0);
5016
5017         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
5018         if (rc == 0) {
5019                 LASSERT(!(lma->lma_compat & LMAC_NOT_IN_OI));
5020
5021                 if (fid_is_sane(fid)) {
5022                         /* FID-in-dirent is valid. */
5023                         if (lu_fid_eq(fid, &lma->lma_self_fid))
5024                                 GOTO(out_inode, rc = 0);
5025
5026                         /* Do not repair under dryrun mode. */
5027                         if (*attr & LUDA_VERIFY_DRYRUN) {
5028                                 *attr |= LUDA_REPAIR;
5029                                 GOTO(out_inode, rc = 0);
5030                         }
5031
5032                         if (!dev->od_dirent_journal) {
5033                                 iput(inode);
5034                                 brelse(bh);
5035                                 if (hlock != NULL)
5036                                         ldiskfs_htree_unlock(hlock);
5037                                 else
5038                                         up_read(&obj->oo_ext_idx_sem);
5039                                 dev->od_dirent_journal = 1;
5040                                 goto again;
5041                         }
5042
5043                         *fid = lma->lma_self_fid;
5044                         dirty = true;
5045                         /* Update the FID-in-dirent. */
5046                         rc = osd_dirent_update(jh, sb, ent, fid, bh, de);
5047                         if (rc == 0)
5048                                 *attr |= LUDA_REPAIR;
5049                 } else {
5050                         /* Do not repair under dryrun mode. */
5051                         if (*attr & LUDA_VERIFY_DRYRUN) {
5052                                 *fid = lma->lma_self_fid;
5053                                 *attr |= LUDA_REPAIR;
5054                                 GOTO(out_inode, rc = 0);
5055                         }
5056
5057                         if (!dev->od_dirent_journal) {
5058                                 iput(inode);
5059                                 brelse(bh);
5060                                 if (hlock != NULL)
5061                                         ldiskfs_htree_unlock(hlock);
5062                                 else
5063                                         up_read(&obj->oo_ext_idx_sem);
5064                                 dev->od_dirent_journal = 1;
5065                                 goto again;
5066                         }
5067
5068                         *fid = lma->lma_self_fid;
5069                         dirty = true;
5070                         /* Append the FID-in-dirent. */
5071                         rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5072                                                  fid, bh, de, hlock);
5073                         if (rc == 0)
5074                                 *attr |= LUDA_REPAIR;
5075                 }
5076         } else if (rc == -ENODATA) {
5077                 /* Do not repair under dryrun mode. */
5078                 if (*attr & LUDA_VERIFY_DRYRUN) {
5079                         if (fid_is_sane(fid)) {
5080                                 *attr |= LUDA_REPAIR;
5081                         } else {
5082                                 lu_igif_build(fid, inode->i_ino,
5083                                               inode->i_generation);
5084                                 *attr |= LUDA_UPGRADE;
5085                         }
5086                         GOTO(out_inode, rc = 0);
5087                 }
5088
5089                 if (!dev->od_dirent_journal) {
5090                         iput(inode);
5091                         brelse(bh);
5092                         if (hlock != NULL)
5093                                 ldiskfs_htree_unlock(hlock);
5094                         else
5095                                 up_read(&obj->oo_ext_idx_sem);
5096                         dev->od_dirent_journal = 1;
5097                         goto again;
5098                 }
5099
5100                 dirty = true;
5101                 if (unlikely(fid_is_sane(fid))) {
5102                         /* FID-in-dirent exists, but FID-in-LMA is lost.
5103                          * Trust the FID-in-dirent, and add FID-in-LMA. */
5104                         rc = osd_ea_fid_set(info, inode, fid, 0, 0);
5105                         if (rc == 0)
5106                                 *attr |= LUDA_REPAIR;
5107                 } else {
5108                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
5109                         /* It is probably IGIF object. Only aappend the
5110                          * FID-in-dirent. OI scrub will process FID-in-LMA. */
5111                         rc = osd_dirent_reinsert(env, jh, dir, inode, ent,
5112                                                  fid, bh, de, hlock);
5113                         if (rc == 0)
5114                                 *attr |= LUDA_UPGRADE;
5115                 }
5116         }
5117
5118         GOTO(out_inode, rc);
5119
5120 out_inode:
5121         iput(inode);
5122
5123 out_journal:
5124         brelse(bh);
5125         if (hlock != NULL) {
5126                 ldiskfs_htree_unlock(hlock);
5127         } else {
5128                 if (dev->od_dirent_journal)
5129                         up_write(&obj->oo_ext_idx_sem);
5130                 else
5131                         up_read(&obj->oo_ext_idx_sem);
5132         }
5133         if (jh != NULL)
5134                 ldiskfs_journal_stop(jh);
5135         if (rc >= 0 && !dirty)
5136                 dev->od_dirent_journal = 0;
5137         return rc;
5138 }
5139
5140 /**
5141  * Returns the value at current position from iterator's in memory structure.
5142  *
5143  * \param di struct osd_it_ea, iterator's in memory structure
5144  * \param attr attr requested for dirent.
5145  * \param lde lustre dirent
5146  *
5147  * \retval   0 no error and \param lde has correct lustre dirent.
5148  * \retval -ve on error
5149  */
5150 static inline int osd_it_ea_rec(const struct lu_env *env,
5151                                 const struct dt_it *di,
5152                                 struct dt_rec *dtrec, __u32 attr)
5153 {
5154         struct osd_it_ea       *it    = (struct osd_it_ea *)di;
5155         struct osd_object      *obj   = it->oie_obj;
5156         struct osd_device      *dev   = osd_obj2dev(obj);
5157         struct osd_scrub       *scrub = &dev->od_scrub;
5158         struct scrub_file      *sf    = &scrub->os_file;
5159         struct osd_thread_info *oti   = osd_oti_get(env);
5160         struct osd_inode_id    *id    = &oti->oti_id;
5161         struct osd_idmap_cache *oic   = &oti->oti_cache;
5162         struct lu_fid          *fid   = &it->oie_dirent->oied_fid;
5163         struct lu_dirent       *lde   = (struct lu_dirent *)dtrec;
5164         __u32                   ino   = it->oie_dirent->oied_ino;
5165         int                     rc    = 0;
5166         ENTRY;
5167
5168         if (attr & LUDA_VERIFY) {
5169                 attr |= LUDA_TYPE;
5170                 if (unlikely(ino == osd_sb(dev)->s_root->d_inode->i_ino)) {
5171                         attr |= LUDA_IGNORE;
5172                         rc = 0;
5173                 } else {
5174                         rc = osd_dirent_check_repair(env, obj, it, fid, id,
5175                                                      &attr);
5176                 }
5177         } else {
5178                 attr &= ~LU_DIRENT_ATTRS_MASK;
5179                 if (!fid_is_sane(fid)) {
5180                         if (OBD_FAIL_CHECK(OBD_FAIL_FID_LOOKUP))
5181                                 RETURN(-ENOENT);
5182
5183                         rc = osd_ea_fid_get(env, obj, ino, fid, id);
5184                 } else {
5185                         osd_id_gen(id, ino, OSD_OII_NOGEN);
5186                 }
5187         }
5188
5189         /* Pack the entry anyway, at least the offset is right. */
5190         osd_it_pack_dirent(lde, fid, it->oie_dirent->oied_off,
5191                            it->oie_dirent->oied_name,
5192                            it->oie_dirent->oied_namelen,
5193                            it->oie_dirent->oied_type, attr);
5194
5195         if (rc < 0)
5196                 RETURN(rc);
5197
5198         if (osd_remote_fid(env, dev, fid))
5199                 RETURN(0);
5200
5201         if (likely(!(attr & LUDA_IGNORE)))
5202                 rc = osd_add_oi_cache(oti, dev, id, fid);
5203
5204         if (!(attr & LUDA_VERIFY) &&
5205             (scrub->os_pos_current <= ino) &&
5206             ((sf->sf_flags & SF_INCONSISTENT) ||
5207              (sf->sf_flags & SF_UPGRADE && fid_is_igif(fid)) ||
5208              ldiskfs_test_bit(osd_oi_fid2idx(dev, fid), sf->sf_oi_bitmap)))
5209                 osd_consistency_check(oti, dev, oic);
5210
5211         RETURN(rc);
5212 }
5213
5214 /**
5215  * Returns a cookie for current position of the iterator head, so that
5216  * user can use this cookie to load/start the iterator next time.
5217  *
5218  * \param di iterator's in memory structure
5219  *
5220  * \retval cookie for current position, on success
5221  */
5222 static __u64 osd_it_ea_store(const struct lu_env *env, const struct dt_it *di)
5223 {
5224         struct osd_it_ea *it = (struct osd_it_ea *)di;
5225
5226         return it->oie_dirent->oied_off;
5227 }
5228
5229 /**
5230  * It calls osd_ldiskfs_it_fill() which will use ->readdir()
5231  * to load a directory entry at a time and stored it i inn,
5232  * in iterator's in-memory data structure.
5233  *
5234  * \param di struct osd_it_ea, iterator's in memory structure
5235  *
5236  * \retval +ve on success
5237  * \retval -ve on error
5238  */
5239 static int osd_it_ea_load(const struct lu_env *env,
5240                           const struct dt_it *di, __u64 hash)
5241 {
5242         struct osd_it_ea *it = (struct osd_it_ea *)di;
5243         int rc;
5244
5245         ENTRY;
5246         it->oie_file.f_pos = hash;
5247
5248         rc =  osd_ldiskfs_it_fill(env, di);
5249         if (rc == 0)
5250                 rc = +1;
5251
5252         RETURN(rc);
5253 }
5254
5255 /**
5256  * Index lookup function for interoperability mode (b11826).
5257  *
5258  * \param key,  key i.e. file name to be searched
5259  *
5260  * \retval +ve, on success
5261  * \retval -ve, on error
5262  */
5263 static int osd_index_ea_lookup(const struct lu_env *env, struct dt_object *dt,
5264                                struct dt_rec *rec, const struct dt_key *key,
5265                                struct lustre_capa *capa)
5266 {
5267         struct osd_object *obj = osd_dt_obj(dt);
5268         int rc = 0;
5269
5270         ENTRY;
5271
5272         LASSERT(S_ISDIR(obj->oo_inode->i_mode));
5273         LINVRNT(osd_invariant(obj));
5274
5275         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
5276                 return -EACCES;
5277
5278         rc = osd_ea_lookup_rec(env, obj, rec, key);
5279         if (rc == 0)
5280                 rc = +1;
5281         RETURN(rc);
5282 }
5283
5284 /**
5285  * Index and Iterator operations for interoperability
5286  * mode (i.e. to run 2.0 mds on 1.8 disk) (b11826)
5287  */
5288 static const struct dt_index_operations osd_index_ea_ops = {
5289         .dio_lookup         = osd_index_ea_lookup,
5290         .dio_declare_insert = osd_index_declare_ea_insert,
5291         .dio_insert         = osd_index_ea_insert,
5292         .dio_declare_delete = osd_index_declare_ea_delete,
5293         .dio_delete         = osd_index_ea_delete,
5294         .dio_it     = {
5295                 .init     = osd_it_ea_init,
5296                 .fini     = osd_it_ea_fini,
5297                 .get      = osd_it_ea_get,
5298                 .put      = osd_it_ea_put,
5299                 .next     = osd_it_ea_next,
5300                 .key      = osd_it_ea_key,
5301                 .key_size = osd_it_ea_key_size,
5302                 .rec      = osd_it_ea_rec,
5303                 .store    = osd_it_ea_store,
5304                 .load     = osd_it_ea_load
5305         }
5306 };
5307
5308 static void *osd_key_init(const struct lu_context *ctx,
5309                           struct lu_context_key *key)
5310 {
5311         struct osd_thread_info *info;
5312
5313         OBD_ALLOC_PTR(info);
5314         if (info == NULL)
5315                 return ERR_PTR(-ENOMEM);
5316
5317         OBD_ALLOC(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5318         if (info->oti_it_ea_buf == NULL)
5319                 goto out_free_info;
5320
5321         info->oti_env = container_of(ctx, struct lu_env, le_ctx);
5322
5323         info->oti_hlock = ldiskfs_htree_lock_alloc();
5324         if (info->oti_hlock == NULL)
5325                 goto out_free_ea;
5326
5327         return info;
5328
5329  out_free_ea:
5330         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5331  out_free_info:
5332         OBD_FREE_PTR(info);
5333         return ERR_PTR(-ENOMEM);
5334 }
5335
5336 static void osd_key_fini(const struct lu_context *ctx,
5337                          struct lu_context_key *key, void* data)
5338 {
5339         struct osd_thread_info *info = data;
5340
5341         if (info->oti_hlock != NULL)
5342                 ldiskfs_htree_lock_free(info->oti_hlock);
5343         OBD_FREE(info->oti_it_ea_buf, OSD_IT_EA_BUFSIZE);
5344         lu_buf_free(&info->oti_iobuf.dr_pg_buf);
5345         lu_buf_free(&info->oti_iobuf.dr_bl_buf);
5346         OBD_FREE_PTR(info);
5347 }
5348
5349 static void osd_key_exit(const struct lu_context *ctx,
5350                          struct lu_context_key *key, void *data)
5351 {
5352         struct osd_thread_info *info = data;
5353
5354         LASSERT(info->oti_r_locks == 0);
5355         LASSERT(info->oti_w_locks == 0);
5356         LASSERT(info->oti_txns    == 0);
5357 }
5358
5359 /* type constructor/destructor: osd_type_init, osd_type_fini */
5360 LU_TYPE_INIT_FINI(osd, &osd_key);
5361
5362 struct lu_context_key osd_key = {
5363         .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
5364         .lct_init = osd_key_init,
5365         .lct_fini = osd_key_fini,
5366         .lct_exit = osd_key_exit
5367 };
5368
5369
5370 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
5371                            const char *name, struct lu_device *next)
5372 {
5373         struct osd_device *osd = osd_dev(d);
5374
5375         if (strlcpy(osd->od_svname, name, sizeof(osd->od_svname))
5376             >= sizeof(osd->od_svname))
5377                 return -E2BIG;
5378         return osd_procfs_init(osd, name);
5379 }
5380
5381 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
5382 {
5383         ENTRY;
5384
5385         /* shutdown quota slave instance associated with the device */
5386         if (o->od_quota_slave != NULL) {
5387                 qsd_fini(env, o->od_quota_slave);
5388                 o->od_quota_slave = NULL;
5389         }
5390
5391         RETURN(0);
5392 }
5393
5394 static void osd_umount(const struct lu_env *env, struct osd_device *o)
5395 {
5396         ENTRY;
5397
5398         if (o->od_mnt != NULL) {
5399                 shrink_dcache_sb(osd_sb(o));
5400                 osd_sync(env, &o->od_dt_dev);
5401
5402                 mntput(o->od_mnt);
5403                 o->od_mnt = NULL;
5404         }
5405
5406         EXIT;
5407 }
5408
5409 static int osd_mount(const struct lu_env *env,
5410                      struct osd_device *o, struct lustre_cfg *cfg)
5411 {
5412         const char              *name  = lustre_cfg_string(cfg, 0);
5413         const char              *dev  = lustre_cfg_string(cfg, 1);
5414         const char              *opts;
5415         unsigned long            page, s_flags, lmd_flags = 0;
5416         struct page             *__page;
5417         struct file_system_type *type;
5418         char                    *options = NULL;
5419         char                    *str;
5420         struct osd_thread_info  *info = osd_oti_get(env);
5421         struct lu_fid           *fid = &info->oti_fid;
5422         struct inode            *inode;
5423         int                      rc = 0;
5424         ENTRY;
5425
5426         if (o->od_mnt != NULL)
5427                 RETURN(0);
5428
5429         if (strlen(dev) >= sizeof(o->od_mntdev))
5430                 RETURN(-E2BIG);
5431         strcpy(o->od_mntdev, dev);
5432
5433         OBD_PAGE_ALLOC(__page, GFP_IOFS);
5434         if (__page == NULL)
5435                 GOTO(out, rc = -ENOMEM);
5436
5437         str = lustre_cfg_string(cfg, 2);
5438         s_flags = simple_strtoul(str, NULL, 0);
5439         str = strstr(str, ":");
5440         if (str)
5441                 lmd_flags = simple_strtoul(str + 1, NULL, 0);
5442         opts = lustre_cfg_string(cfg, 3);
5443         page = (unsigned long)page_address(__page);
5444         options = (char *)page;
5445         *options = '\0';
5446         if (opts == NULL)
5447                 strcat(options, "user_xattr,acl");
5448         else
5449                 strcat(options, opts);
5450
5451         /* Glom up mount options */
5452         if (*options != '\0')
5453                 strcat(options, ",");
5454         strlcat(options, "no_mbcache", PAGE_CACHE_SIZE);
5455
5456         type = get_fs_type("ldiskfs");
5457         if (!type) {
5458                 CERROR("%s: cannot find ldiskfs module\n", name);
5459                 GOTO(out, rc = -ENODEV);
5460         }
5461
5462         o->od_mnt = vfs_kern_mount(type, s_flags, dev, options);
5463         module_put(type->owner);
5464
5465         if (IS_ERR(o->od_mnt)) {
5466                 rc = PTR_ERR(o->od_mnt);
5467                 o->od_mnt = NULL;
5468                 CERROR("%s: can't mount %s: %d\n", name, dev, rc);
5469                 GOTO(out, rc);
5470         }
5471
5472 #ifdef HAVE_DEV_SET_RDONLY
5473         if (dev_check_rdonly(o->od_mnt->mnt_sb->s_bdev)) {
5474                 CERROR("%s: underlying device %s is marked as read-only. "
5475                        "Setup failed\n", name, dev);
5476                 GOTO(out_mnt, rc = -EROFS);
5477         }
5478 #endif
5479
5480         if (!LDISKFS_HAS_COMPAT_FEATURE(o->od_mnt->mnt_sb,
5481             LDISKFS_FEATURE_COMPAT_HAS_JOURNAL)) {
5482                 CERROR("%s: device %s is mounted w/o journal\n", name, dev);
5483                 GOTO(out_mnt, rc = -EINVAL);
5484         }
5485
5486         inode = osd_sb(o)->s_root->d_inode;
5487         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
5488         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
5489         rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
5490         if (rc != 0) {
5491                 CERROR("%s: failed to set lma on %s root inode\n", name, dev);
5492                 GOTO(out_mnt, rc);
5493         }
5494
5495         if (lmd_flags & LMD_FLG_NOSCRUB)
5496                 o->od_noscrub = 1;
5497
5498         GOTO(out, rc = 0);
5499
5500 out_mnt:
5501         mntput(o->od_mnt);
5502         o->od_mnt = NULL;
5503
5504 out:
5505         if (__page)
5506                 OBD_PAGE_FREE(__page);
5507
5508         return rc;
5509 }
5510
5511 static struct lu_device *osd_device_fini(const struct lu_env *env,
5512                                          struct lu_device *d)
5513 {
5514         struct osd_device *o = osd_dev(d);
5515         ENTRY;
5516
5517         osd_procfs_fini(o);
5518         osd_shutdown(env, o);
5519         osd_scrub_cleanup(env, o);
5520         osd_obj_map_fini(o);
5521         osd_umount(env, o);
5522
5523         RETURN(NULL);
5524 }
5525
5526 static int osd_device_init0(const struct lu_env *env,
5527                             struct osd_device *o,
5528                             struct lustre_cfg *cfg)
5529 {
5530         struct lu_device        *l = osd2lu_dev(o);
5531         struct osd_thread_info *info;
5532         int                     rc;
5533         int                     cplen = 0;
5534
5535         /* if the module was re-loaded, env can loose its keys */
5536         rc = lu_env_refill((struct lu_env *) env);
5537         if (rc)
5538                 GOTO(out, rc);
5539         info = osd_oti_get(env);
5540         LASSERT(info);
5541
5542         l->ld_ops = &osd_lu_ops;
5543         o->od_dt_dev.dd_ops = &osd_dt_ops;
5544
5545         spin_lock_init(&o->od_osfs_lock);
5546         mutex_init(&o->od_otable_mutex);
5547         o->od_osfs_age = cfs_time_shift_64(-1000);
5548
5549         o->od_capa_hash = init_capa_hash();
5550         if (o->od_capa_hash == NULL)
5551                 GOTO(out, rc = -ENOMEM);
5552
5553         o->od_read_cache = 1;
5554         o->od_writethrough_cache = 1;
5555         o->od_readcache_max_filesize = OSD_MAX_CACHE_SIZE;
5556
5557         rc = osd_mount(env, o, cfg);
5558         if (rc)
5559                 GOTO(out_capa, rc);
5560
5561         cplen = strlcpy(o->od_svname, lustre_cfg_string(cfg, 4),
5562                         sizeof(o->od_svname));
5563         if (cplen >= sizeof(o->od_svname)) {
5564                 rc = -E2BIG;
5565                 GOTO(out_mnt, rc);
5566         }
5567
5568         if (server_name_is_ost(o->od_svname))
5569                 o->od_is_ost = 1;
5570
5571         rc = osd_obj_map_init(env, o);
5572         if (rc != 0)
5573                 GOTO(out_mnt, rc);
5574
5575         rc = lu_site_init(&o->od_site, l);
5576         if (rc != 0)
5577                 GOTO(out_compat, rc);
5578         o->od_site.ls_bottom_dev = l;
5579
5580         rc = lu_site_init_finish(&o->od_site);
5581         if (rc != 0)
5582                 GOTO(out_site, rc);
5583
5584         CFS_INIT_LIST_HEAD(&o->od_ios_list);
5585         /* setup scrub, including OI files initialization */
5586         rc = osd_scrub_setup(env, o);
5587         if (rc < 0)
5588                 GOTO(out_site, rc);
5589
5590         rc = osd_procfs_init(o, o->od_svname);
5591         if (rc != 0) {
5592                 CERROR("%s: can't initialize procfs: rc = %d\n",
5593                        o->od_svname, rc);
5594                 GOTO(out_scrub, rc);
5595         }
5596
5597         LASSERT(l->ld_site->ls_linkage.next && l->ld_site->ls_linkage.prev);
5598
5599         /* initialize quota slave instance */
5600         o->od_quota_slave = qsd_init(env, o->od_svname, &o->od_dt_dev,
5601                                      o->od_proc_entry);
5602         if (IS_ERR(o->od_quota_slave)) {
5603                 rc = PTR_ERR(o->od_quota_slave);
5604                 o->od_quota_slave = NULL;
5605                 GOTO(out_procfs, rc);
5606         }
5607
5608         RETURN(0);
5609
5610 out_procfs:
5611         osd_procfs_fini(o);
5612 out_scrub:
5613         osd_scrub_cleanup(env, o);
5614 out_site:
5615         lu_site_fini(&o->od_site);
5616 out_compat:
5617         osd_obj_map_fini(o);
5618 out_mnt:
5619         osd_umount(env, o);
5620 out_capa:
5621         cleanup_capa_hash(o->od_capa_hash);
5622 out:
5623         return rc;
5624 }
5625
5626 static struct lu_device *osd_device_alloc(const struct lu_env *env,
5627                                           struct lu_device_type *t,
5628                                           struct lustre_cfg *cfg)
5629 {
5630         struct osd_device *o;
5631         int                rc;
5632
5633         OBD_ALLOC_PTR(o);
5634         if (o == NULL)
5635                 return ERR_PTR(-ENOMEM);
5636
5637         rc = dt_device_init(&o->od_dt_dev, t);
5638         if (rc == 0) {
5639                 /* Because the ctx might be revived in dt_device_init,
5640                  * refill the env here */
5641                 lu_env_refill((struct lu_env *)env);
5642                 rc = osd_device_init0(env, o, cfg);
5643                 if (rc)
5644                         dt_device_fini(&o->od_dt_dev);
5645         }
5646
5647         if (unlikely(rc != 0))
5648                 OBD_FREE_PTR(o);
5649
5650         return rc == 0 ? osd2lu_dev(o) : ERR_PTR(rc);
5651 }
5652
5653 static struct lu_device *osd_device_free(const struct lu_env *env,
5654                                          struct lu_device *d)
5655 {
5656         struct osd_device *o = osd_dev(d);
5657         ENTRY;
5658
5659         cleanup_capa_hash(o->od_capa_hash);
5660         /* XXX: make osd top device in order to release reference */
5661         d->ld_site->ls_top_dev = d;
5662         lu_site_purge(env, d->ld_site, -1);
5663         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
5664                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
5665                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
5666         }
5667         lu_site_fini(&o->od_site);
5668         dt_device_fini(&o->od_dt_dev);
5669         OBD_FREE_PTR(o);
5670         RETURN(NULL);
5671 }
5672
5673 static int osd_process_config(const struct lu_env *env,
5674                               struct lu_device *d, struct lustre_cfg *cfg)
5675 {
5676         struct osd_device *o = osd_dev(d);
5677         int err;
5678         ENTRY;
5679
5680         switch(cfg->lcfg_command) {
5681         case LCFG_SETUP:
5682                 err = osd_mount(env, o, cfg);
5683                 break;
5684         case LCFG_CLEANUP:
5685                 lu_dev_del_linkage(d->ld_site, d);
5686                 err = osd_shutdown(env, o);
5687                 break;
5688         default:
5689                 err = -ENOSYS;
5690         }
5691
5692         RETURN(err);
5693 }
5694
5695 static int osd_recovery_complete(const struct lu_env *env,
5696                                  struct lu_device *d)
5697 {
5698         struct osd_device       *osd = osd_dev(d);
5699         int                      rc = 0;
5700         ENTRY;
5701
5702         if (osd->od_quota_slave == NULL)
5703                 RETURN(0);
5704
5705         /* start qsd instance on recovery completion, this notifies the quota
5706          * slave code that we are about to process new requests now */
5707         rc = qsd_start(env, osd->od_quota_slave);
5708         RETURN(rc);
5709 }
5710
5711 /*
5712  * we use exports to track all osd users
5713  */
5714 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
5715                            struct obd_device *obd, struct obd_uuid *cluuid,
5716                            struct obd_connect_data *data, void *localdata)
5717 {
5718         struct osd_device    *osd = osd_dev(obd->obd_lu_dev);
5719         struct lustre_handle  conn;
5720         int                   rc;
5721         ENTRY;
5722
5723         CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
5724
5725         rc = class_connect(&conn, obd, cluuid);
5726         if (rc)
5727                 RETURN(rc);
5728
5729         *exp = class_conn2export(&conn);
5730
5731         spin_lock(&osd->od_osfs_lock);
5732         osd->od_connects++;
5733         spin_unlock(&osd->od_osfs_lock);
5734
5735         RETURN(0);
5736 }
5737
5738 /*
5739  * once last export (we don't count self-export) disappeared
5740  * osd can be released
5741  */
5742 static int osd_obd_disconnect(struct obd_export *exp)
5743 {
5744         struct obd_device *obd = exp->exp_obd;
5745         struct osd_device *osd = osd_dev(obd->obd_lu_dev);
5746         int                rc, release = 0;
5747         ENTRY;
5748
5749         /* Only disconnect the underlying layers on the final disconnect. */
5750         spin_lock(&osd->od_osfs_lock);
5751         osd->od_connects--;
5752         if (osd->od_connects == 0)
5753                 release = 1;
5754         spin_unlock(&osd->od_osfs_lock);
5755
5756         rc = class_disconnect(exp); /* bz 9811 */
5757
5758         if (rc == 0 && release)
5759                 class_manual_cleanup(obd);
5760         RETURN(rc);
5761 }
5762
5763 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
5764                        struct lu_device *dev)
5765 {
5766         struct osd_device *osd = osd_dev(dev);
5767         int                result = 0;
5768         ENTRY;
5769
5770         if (osd->od_quota_slave != NULL)
5771                 /* set up quota slave objects */
5772                 result = qsd_prepare(env, osd->od_quota_slave);
5773
5774         RETURN(result);
5775 }
5776
5777 static const struct lu_object_operations osd_lu_obj_ops = {
5778         .loo_object_init      = osd_object_init,
5779         .loo_object_delete    = osd_object_delete,
5780         .loo_object_release   = osd_object_release,
5781         .loo_object_free      = osd_object_free,
5782         .loo_object_print     = osd_object_print,
5783         .loo_object_invariant = osd_object_invariant
5784 };
5785
5786 const struct lu_device_operations osd_lu_ops = {
5787         .ldo_object_alloc      = osd_object_alloc,
5788         .ldo_process_config    = osd_process_config,
5789         .ldo_recovery_complete = osd_recovery_complete,
5790         .ldo_prepare           = osd_prepare,
5791 };
5792
5793 static const struct lu_device_type_operations osd_device_type_ops = {
5794         .ldto_init = osd_type_init,
5795         .ldto_fini = osd_type_fini,
5796
5797         .ldto_start = osd_type_start,
5798         .ldto_stop  = osd_type_stop,
5799
5800         .ldto_device_alloc = osd_device_alloc,
5801         .ldto_device_free  = osd_device_free,
5802
5803         .ldto_device_init    = osd_device_init,
5804         .ldto_device_fini    = osd_device_fini
5805 };
5806
5807 struct lu_device_type osd_device_type = {
5808         .ldt_tags     = LU_DEVICE_DT,
5809         .ldt_name     = LUSTRE_OSD_LDISKFS_NAME,
5810         .ldt_ops      = &osd_device_type_ops,
5811         .ldt_ctx_tags = LCT_LOCAL,
5812 };
5813
5814 /*
5815  * lprocfs legacy support.
5816  */
5817 static struct obd_ops osd_obd_device_ops = {
5818         .o_owner = THIS_MODULE,
5819         .o_connect      = osd_obd_connect,
5820         .o_disconnect   = osd_obd_disconnect
5821 };
5822
5823 static int __init osd_mod_init(void)
5824 {
5825         struct lprocfs_static_vars lvars;
5826         int rc;
5827
5828         osd_oi_mod_init();
5829         lprocfs_osd_init_vars(&lvars);
5830
5831         rc = lu_kmem_init(ldiskfs_caches);
5832         if (rc)
5833                 return rc;
5834
5835         rc = class_register_type(&osd_obd_device_ops, NULL, lvars.module_vars,
5836                                  LUSTRE_OSD_LDISKFS_NAME, &osd_device_type);
5837         if (rc)
5838                 lu_kmem_fini(ldiskfs_caches);
5839         return rc;
5840 }
5841
5842 static void __exit osd_mod_exit(void)
5843 {
5844         class_unregister_type(LUSTRE_OSD_LDISKFS_NAME);
5845         lu_kmem_fini(ldiskfs_caches);
5846 }
5847
5848 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5849 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_LDISKFS_NAME")");
5850 MODULE_LICENSE("GPL");
5851
5852 cfs_module(osd, "0.1.0", osd_mod_init, osd_mod_exit);