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