Whamcloud - gitweb
d8190a8f4b67d62a8aa5e793abf6b43f16781a8f
[fs/lustre-release.git] / lustre / osd-zfs / osd_oi.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_oi.c
33  * OI functions to map fid to dnode
34  *
35  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36  * Author: Mike Pershin <tappro@whamcloud.com>
37  * Author: Di Wang <di.wang@intel.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_OSD
41
42 #include <lustre_ver.h>
43 #include <libcfs/libcfs.h>
44 #include <obd_support.h>
45 #include <lustre_net.h>
46 #include <obd.h>
47 #include <obd_class.h>
48 #include <lustre_disk.h>
49 #include <lustre_fid.h>
50
51 #include "osd_internal.h"
52
53 #include <sys/dnode.h>
54 #include <sys/dbuf.h>
55 #include <sys/spa.h>
56 #include <sys/stat.h>
57 #include <sys/zap.h>
58 #include <sys/spa_impl.h>
59 #include <sys/zfs_znode.h>
60 #include <sys/dmu_tx.h>
61 #include <sys/dmu_objset.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/sa_impl.h>
64 #include <sys/txg.h>
65
66 #define OSD_OI_FID_NR         (1UL << 7)
67 #define OSD_OI_FID_NR_MAX     (1UL << OSD_OI_FID_OID_BITS_MAX)
68 unsigned int osd_oi_count = OSD_OI_FID_NR;
69
70
71 /*
72  * zfs osd maintains names for known fids in the name hierarchy
73  * so that one can mount filesystem with regular ZFS stack and
74  * access files
75  */
76 struct named_oid {
77         unsigned long    oid;
78         char            *name;
79 };
80
81 static const struct named_oid oids[] = {
82         { .oid = LAST_RECV_OID,        .name = LAST_RCVD },
83         { .oid = OFD_LAST_GROUP_OID,   .name = "LAST_GROUP" },
84         { .oid = LLOG_CATALOGS_OID,    .name = "CATALOGS" },
85         { .oid = MGS_CONFIGS_OID,      /*MOUNT_CONFIGS_DIR*/ },
86         { .oid = FID_SEQ_SRV_OID,      .name = "seq_srv" },
87         { .oid = FID_SEQ_CTL_OID,      .name = "seq_ctl" },
88         { .oid = FLD_INDEX_OID,        .name = "fld" },
89         { .oid = MDD_LOV_OBJ_OID,      .name = LOV_OBJID },
90         { .oid = OFD_HEALTH_CHECK_OID, .name = HEALTH_CHECK },
91         { .oid = ACCT_USER_OID,        .name = "acct_usr_inode" },
92         { .oid = ACCT_GROUP_OID,       .name = "acct_grp_inode" },
93         { .oid = ACCT_PROJECT_OID,     .name = "acct_prj_inode" },
94         { .oid = REPLY_DATA_OID,       .name = REPLY_DATA },
95         { .oid = 0 }
96 };
97
98 static char *oid2name(const unsigned long oid)
99 {
100         int i = 0;
101
102         while (oids[i].oid) {
103                 if (oids[i].oid == oid)
104                         return oids[i].name;
105                 i++;
106         }
107         return NULL;
108 }
109
110 /**
111  * Lookup an existing OI by the given name.
112  */
113 static int
114 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
115               uint64_t parent, const char *name, struct osd_oi *oi)
116 {
117         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
118         int                      rc;
119
120         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
121         if (rc)
122                 return rc;
123
124         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
125         if (rc >= sizeof(oi->oi_name))
126                 return -E2BIG;
127
128         oi->oi_zapid = zde->zde_dnode;
129
130         return 0;
131 }
132
133 /**
134  * Create a new OI with the given name.
135  */
136 static int
137 osd_oi_create(const struct lu_env *env, struct osd_device *o,
138               uint64_t parent, const char *name, uint64_t *child)
139 {
140         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
141         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
142         sa_handle_t             *sa_hdl = NULL;
143         dmu_tx_t                *tx;
144         uint64_t                 oid;
145         int                      rc;
146
147         /* verify it doesn't already exist */
148         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
149         if (rc == 0)
150                 return -EEXIST;
151
152         if (o->od_dt_dev.dd_rdonly)
153                 return -EROFS;
154
155         /* create fid-to-dnode index */
156         tx = dmu_tx_create(o->od_os);
157         if (tx == NULL)
158                 return -ENOMEM;
159
160         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
161         dmu_tx_hold_bonus(tx, parent);
162         dmu_tx_hold_zap(tx, parent, TRUE, name);
163         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
164
165         rc = -dmu_tx_assign(tx, TXG_WAIT);
166         if (rc) {
167                 dmu_tx_abort(tx);
168                 return rc;
169         }
170
171         oid = osd_zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
172                                    DMU_OT_DIRECTORY_CONTENTS,
173                                    14, /* == ZFS fzap_default_block_shift */
174                                    DN_MAX_INDBLKSHIFT,
175                                    0, tx);
176
177         rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
178         if (rc)
179                 goto commit;
180         la->la_valid = LA_MODE | LA_UID | LA_GID;
181         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
182         la->la_uid = la->la_gid = 0;
183         rc = __osd_attr_init(env, o, sa_hdl, tx, la, parent);
184         sa_handle_destroy(sa_hdl);
185         if (rc)
186                 goto commit;
187
188         zde->zde_dnode = oid;
189         zde->zde_pad = 0;
190         zde->zde_type = IFTODT(S_IFDIR);
191
192         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
193
194 commit:
195         if (rc)
196                 dmu_object_free(o->od_os, oid, tx);
197         dmu_tx_commit(tx);
198
199         if (rc == 0)
200                 *child = oid;
201
202         return rc;
203 }
204
205 static int
206 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
207                       uint64_t parent, const char *name, uint64_t *child)
208 {
209         struct osd_oi   oi;
210         int             rc;
211
212         rc = osd_oi_lookup(env, o, parent, name, &oi);
213         if (rc == 0)
214                 *child = oi.oi_zapid;
215         else if (rc == -ENOENT)
216                 rc = osd_oi_create(env, o, parent, name, child);
217
218         return rc;
219 }
220
221 /**
222  * Lookup the target index/flags of the fid, so it will know where
223  * the object is located (tgt index) and it is MDT or OST object.
224  */
225 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
226                    u64 seq, struct lu_seq_range *range)
227 {
228         struct seq_server_site  *ss = osd_seq_site(osd);
229
230         if (fid_seq_is_idif(seq)) {
231                 fld_range_set_ost(range);
232                 range->lsr_index = idif_ost_idx(seq);
233                 return 0;
234         }
235
236         if (!fid_seq_in_fldb(seq)) {
237                 fld_range_set_mdt(range);
238                 if (ss != NULL)
239                         /* FIXME: If ss is NULL, it suppose not get lsr_index
240                          * at all */
241                         range->lsr_index = ss->ss_node_id;
242                 return 0;
243         }
244
245         LASSERT(ss != NULL);
246         fld_range_set_any(range);
247         /* OSD will only do local fld lookup */
248         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
249 }
250
251 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
252                   const struct lu_fid *fid)
253 {
254         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
255         int                     rc;
256         ENTRY;
257
258         if (fid_is_idif(fid))
259                 RETURN(1);
260
261         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
262                      fid_is_name_llog(fid) || fid_is_quota(fid))
263                 RETURN(0);
264
265         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
266         if (rc != 0) {
267                 /* During upgrade, OST FLDB might not be loaded because
268                  * OST FLDB is not created until 2.6, so if some DNE
269                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
270                  * not be able to find the sequence from local FLDB
271                  * cache see fld_index_init(). */
272                 if (rc == -ENOENT && osd->od_is_ost)
273                         RETURN(1);
274
275                 if (rc != -ENOENT)
276                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
277                                osd_name(osd), PFID(fid), rc);
278                 RETURN(0);
279         }
280
281         if (fld_range_is_ost(range))
282                 RETURN(1);
283
284         RETURN(0);
285 }
286
287 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
288                                            u64 seq)
289 {
290         struct osd_seq *osd_seq;
291
292         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
293                 if (osd_seq->os_seq == seq)
294                         return osd_seq;
295         }
296         return NULL;
297 }
298
299 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
300 {
301         struct osd_seq *osd_seq;
302
303         read_lock(&seq_list->osl_seq_list_lock);
304         osd_seq = osd_seq_find_locked(seq_list, seq);
305         read_unlock(&seq_list->osl_seq_list_lock);
306
307         return osd_seq;
308 }
309
310 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
311                                            struct osd_device *osd, u64 seq)
312 {
313         struct osd_seq_list     *seq_list = &osd->od_seq_list;
314         struct osd_seq          *osd_seq;
315         char                    *key = osd_oti_get(env)->oti_buf;
316         char                    *seq_name = osd_oti_get(env)->oti_str;
317         struct osd_oi           oi;
318         uint64_t                sdb, odb;
319         int                     i;
320         int                     rc = 0;
321         ENTRY;
322
323         osd_seq = osd_seq_find(seq_list, seq);
324         if (osd_seq != NULL)
325                 RETURN(osd_seq);
326
327         down(&seq_list->osl_seq_init_sem);
328         /* Check again, in case some one else already add it
329          * to the list */
330         osd_seq = osd_seq_find(seq_list, seq);
331         if (osd_seq != NULL)
332                 GOTO(out, rc = 0);
333
334         OBD_ALLOC_PTR(osd_seq);
335         if (osd_seq == NULL)
336                 GOTO(out, rc = -ENOMEM);
337
338         INIT_LIST_HEAD(&osd_seq->os_seq_list);
339         osd_seq->os_seq = seq;
340
341         /* Init subdir count to be 32, but each seq can have
342          * different subdir count */
343         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
344         OBD_ALLOC(osd_seq->os_compat_dirs,
345                   sizeof(uint64_t) * osd_seq->os_subdir_count);
346         if (osd_seq->os_compat_dirs == NULL)
347                 GOTO(out, rc = -ENOMEM);
348
349         oi.oi_zapid = osd->od_O_id;
350         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
351                 fid_seq_is_mdt0(seq)) ?  "%llu" : "%llx",
352                 fid_seq_is_idif(seq) ? 0 : seq);
353
354         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
355         if (rc != 0) {
356                 CERROR("%s: Can not create %s : rc = %d\n",
357                        osd_name(osd), seq_name, rc);
358                 GOTO(out, rc);
359         }
360
361         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
362                 sprintf(key, "d%d", i);
363                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
364                 if (rc)
365                         GOTO(out, rc);
366                 osd_seq->os_compat_dirs[i] = sdb;
367         }
368
369         write_lock(&seq_list->osl_seq_list_lock);
370         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
371         write_unlock(&seq_list->osl_seq_list_lock);
372 out:
373         up(&seq_list->osl_seq_init_sem);
374         if (rc != 0) {
375                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
376                         OBD_FREE(osd_seq->os_compat_dirs,
377                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
378                 if (osd_seq != NULL)
379                         OBD_FREE_PTR(osd_seq);
380                 osd_seq = ERR_PTR(rc);
381         }
382         RETURN(osd_seq);
383 }
384
385 /*
386  * objects w/o a natural reference (unlike a file on a MDS)
387  * are put under a special hierarchy /O/<seq>/d0..dXX
388  * this function returns a directory specific fid belongs to
389  */
390 static uint64_t
391 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
392                         const struct lu_fid *fid, char *buf, int bufsize)
393 {
394         struct osd_seq  *osd_seq;
395         unsigned long   b;
396         u64             id;
397         int             rc;
398
399         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
400         if (IS_ERR(osd_seq)) {
401                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
402                        PFID(fid));
403                 return PTR_ERR(osd_seq);
404         }
405
406         if (fid_is_last_id(fid)) {
407                 id = 0;
408         } else {
409                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
410                 LASSERT(rc == 0); /* we should not get here with IGIF */
411                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
412         }
413
414         b = id % OSD_OST_MAP_SIZE;
415         LASSERT(osd_seq->os_compat_dirs[b]);
416
417         if (buf)
418                 snprintf(buf, bufsize, "%llu", id);
419
420         return osd_seq->os_compat_dirs[b];
421 }
422
423 /* XXX: f_ver is not counted, but may differ too */
424 static void osd_fid2str(char *buf, const struct lu_fid *fid)
425 {
426         sprintf(buf, DFID_NOBRACE, PFID(fid));
427 }
428
429 /*
430  * Determine the zap object id which is being used as the OI for the
431  * given fid.  The lowest N bits in the sequence ID are used as the
432  * index key.  On failure 0 is returned which zfs treats internally
433  * as an invalid object id.
434  */
435 static uint64_t
436 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
437                     char *buf, dnode_t **zdn)
438 {
439         struct osd_oi *oi;
440
441         LASSERT(osd->od_oi_table != NULL);
442         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
443         if (buf)
444                 osd_fid2str(buf, fid);
445         if (zdn)
446                 *zdn = oi->oi_dn;
447
448         return oi->oi_zapid;
449 }
450
451 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
452                             const struct lu_fid *fid, char *buf, int bufsize,
453                             dnode_t **zdn)
454 {
455         uint64_t zapid;
456
457         LASSERT(fid);
458         if (zdn != NULL)
459                 *zdn = NULL;
460
461         if (fid_is_on_ost(env, osd, fid) == 1 || fid_seq(fid) == FID_SEQ_ECHO) {
462                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
463         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
464                 /* special objects with fixed known fids get their name */
465                 char *name = oid2name(fid_oid(fid));
466
467                 if (name) {
468                         zapid = osd->od_root;
469                         if (buf)
470                                 strncpy(buf, name, bufsize);
471                         if (fid_is_acct(fid))
472                                 zapid = MASTER_NODE_OBJ;
473                 } else {
474                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL);
475                 }
476         } else {
477                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn);
478         }
479
480         return zapid;
481 }
482
483 static inline int fid_is_fs_root(const struct lu_fid *fid)
484 {
485         /* Map root inode to special local object FID */
486         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
487                 fid_oid(fid) == OSD_FS_ROOT_OID;
488 }
489
490 static inline int osd_oid(struct osd_device *dev, __u32 local_oid,
491                                uint64_t *oid)
492 {
493         switch (local_oid) {
494         case ACCT_USER_OID:
495                 *oid = dev->od_iusr_oid;
496                 return 0;
497         case ACCT_GROUP_OID:
498                 *oid = dev->od_igrp_oid;
499                 return 0;
500         case ACCT_PROJECT_OID:
501                 /* TODO: real oid */
502                 CERROR("%s: unsupported quota oid: %#x\n",
503                         osd_name(dev), local_oid);
504                 return -ENOTSUPP;
505         }
506
507         return -ENOTSUPP;
508 }
509
510 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
511                    const struct lu_fid *fid, uint64_t *oid)
512 {
513         struct osd_thread_info  *info = osd_oti_get(env);
514         char                    *buf = info->oti_buf;
515         dnode_t *zdn;
516         uint64_t zapid;
517         int                     rc = 0;
518         ENTRY;
519
520         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
521                 RETURN(-ENOENT);
522
523         if (unlikely(fid_is_acct(fid))) {
524                 rc = osd_oid(dev, fid_oid(fid), oid);
525                 if (rc)
526                         RETURN(rc);
527         } else if (unlikely(fid_is_fs_root(fid))) {
528                 *oid = dev->od_root;
529         } else {
530                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
531                                            sizeof(info->oti_buf), &zdn);
532                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
533                                     8, 1, &info->oti_zde);
534                 if (rc)
535                         RETURN(rc);
536                 *oid = info->oti_zde.lzd_reg.zde_dnode;
537         }
538
539         if (rc == 0)
540                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
541                                  ZIO_PRIORITY_ASYNC_READ);
542
543         RETURN(rc);
544 }
545
546 /**
547  * Close an entry in a specific slot.
548  */
549 static void
550 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
551 {
552         struct osd_oi *oi;
553
554         LASSERT(key < o->od_oi_count);
555
556         oi = o->od_oi_table[key];
557         if (oi) {
558                 if (oi->oi_dn)
559                         osd_dnode_rele(oi->oi_dn);
560                 OBD_FREE_PTR(oi);
561                 o->od_oi_table[key] = NULL;
562         }
563 }
564
565 /**
566  * Allocate and open a new entry in the specified unused slot.
567  */
568 static int
569 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
570                  char *name, int key)
571 {
572         struct osd_oi *oi;
573         int rc;
574
575         LASSERT(key < o->od_oi_count);
576         LASSERT(o->od_oi_table[key] == NULL);
577
578         OBD_ALLOC_PTR(oi);
579         if (oi == NULL)
580                 return -ENOMEM;
581
582         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
583         if (rc) {
584                 OBD_FREE_PTR(oi);
585                 return rc;
586         }
587
588         o->od_oi_table[key] = oi;
589         __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
590
591         return 0;
592 }
593
594 /**
595  * Depopulate the OI table.
596  */
597 static void
598 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
599 {
600         int i;
601
602         for (i = 0; i < o->od_oi_count; i++)
603                 osd_oi_remove_table(env, o, i);
604 }
605
606 /**
607  * Populate the OI table based.
608  */
609 static int
610 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
611 {
612         char name[16];
613         int  i, rc = 0;
614         ENTRY;
615
616         for (i = 0; i < count; i++) {
617                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
618                 rc = osd_oi_add_table(env, o, name, i);
619                 if (rc) {
620                         osd_oi_close_table(env, o);
621                         break;
622                 }
623         }
624
625         RETURN(rc);
626 }
627
628 /**
629  * Determine if the type and number of OIs used by this file system.
630  */
631 static int
632 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
633 {
634         uint64_t        root_oid = o->od_root;
635         struct osd_oi   oi;
636         char            name[16];
637         int             rc;
638         ENTRY;
639
640         /*
641          * Check for multiple OIs and determine the count.  There is no
642          * gap handling, if an OI is missing the wrong size can be returned.
643          * The only safeguard is that we know the number of OIs must be a
644          * power of two and this is checked for basic sanity.
645          */
646         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
647                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
648                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
649                 if (rc == 0)
650                         continue;
651
652                 if (rc == -ENOENT) {
653                         if (*count == 0)
654                                 break;
655
656                         if ((*count & (*count - 1)) != 0)
657                                 RETURN(-EDOM);
658
659                         RETURN(0);
660                 }
661
662                 RETURN(rc);
663         }
664
665         /*
666          * No OIs exist, this must be a new filesystem.
667          */
668         *count = 0;
669
670         RETURN(0);
671 }
672
673 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
674 {
675         struct osd_seq_list     *osl = &osd->od_seq_list;
676         struct osd_seq          *osd_seq, *tmp;
677
678         write_lock(&osl->osl_seq_list_lock);
679         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
680                                  os_seq_list) {
681                 list_del(&osd_seq->os_seq_list);
682                 OBD_FREE(osd_seq->os_compat_dirs,
683                          sizeof(uint64_t) * osd_seq->os_subdir_count);
684                 OBD_FREE(osd_seq, sizeof(*osd_seq));
685         }
686         write_unlock(&osl->osl_seq_list_lock);
687
688         return;
689 }
690
691 /**
692  * Create /O subdirectory to map legacy OST objects for compatibility.
693  */
694 static int
695 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
696 {
697         uint64_t         odb, sdb;
698         int              rc;
699         ENTRY;
700
701         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
702         if (rc)
703                 RETURN(rc);
704
705         o->od_O_id = sdb;
706
707         /* Create on-disk indexes to maintain per-UID/GID inode usage.
708          * Those new indexes are created in the top-level ZAP outside the
709          * namespace in order not to confuse ZPL which might interpret those
710          * indexes as directories and assume the values are object IDs */
711         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
712                         oid2name(ACCT_USER_OID), &odb);
713         if (rc)
714                 RETURN(rc);
715         o->od_iusr_oid = odb;
716
717         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
718                         oid2name(ACCT_GROUP_OID), &odb);
719         if (rc)
720                 RETURN(rc);
721         o->od_igrp_oid = odb;
722
723         RETURN(rc);
724 }
725
726 /**
727  * Initialize the OIs by either opening or creating them as needed.
728  */
729 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
730 {
731         char    *key = osd_oti_get(env)->oti_buf;
732         int      i, rc, count = 0;
733         ENTRY;
734
735         rc = osd_oi_probe(env, o, &count);
736         if (rc)
737                 RETURN(rc);
738
739         if (count == 0) {
740                 uint64_t odb, sdb;
741
742                 count = osd_oi_count;
743                 odb = o->od_root;
744
745                 for (i = 0; i < count; i++) {
746                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
747                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
748                         if (rc)
749                                 RETURN(rc);
750                 }
751         }
752
753         rc = osd_oi_init_compat(env, o);
754         if (rc)
755                 RETURN(rc);
756
757         LASSERT((count & (count - 1)) == 0);
758         o->od_oi_count = count;
759         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
760         if (o->od_oi_table == NULL)
761                 RETURN(-ENOMEM);
762
763         rc = osd_oi_open_table(env, o, count);
764         if (rc) {
765                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
766                 o->od_oi_table = NULL;
767         }
768
769         RETURN(rc);
770 }
771
772 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
773 {
774         ENTRY;
775
776         osd_ost_seq_fini(env, o);
777
778         if (o->od_oi_table != NULL) {
779                 (void) osd_oi_close_table(env, o);
780                 OBD_FREE(o->od_oi_table,
781                          sizeof(struct osd_oi *) * o->od_oi_count);
782                 o->od_oi_table = NULL;
783                 o->od_oi_count = 0;
784         }
785
786         EXIT;
787 }
788
789 int osd_options_init(void)
790 {
791         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
792         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
793                 osd_oi_count = OSD_OI_FID_NR;
794
795         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
796                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
797                         osd_oi_count, size_roundup_power2(osd_oi_count));
798                 osd_oi_count = size_roundup_power2(osd_oi_count);
799         }
800
801         return 0;
802 }
803
804 /*
805  * the following set of functions are used to maintain per-thread
806  * cache of FID->ino mapping. this mechanism is used to avoid
807  * expensive LU/OI lookups.
808  */
809 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
810                                      struct osd_device *osd,
811                                      const struct lu_fid *fid)
812 {
813         struct osd_thread_info *oti = osd_oti_get(env);
814         struct osd_idmap_cache *idc = oti->oti_ins_cache;
815         int i;
816
817         for (i = 0; i < oti->oti_ins_cache_used; i++) {
818                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
819                         continue;
820                 if (idc[i].oic_dev != osd)
821                         continue;
822
823                 return idc + i;
824         }
825
826         return NULL;
827 }
828
829 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
830                                     struct osd_device *osd,
831                                     const struct lu_fid *fid)
832 {
833         struct osd_thread_info *oti = osd_oti_get(env);
834         struct osd_idmap_cache *idc;
835         int i;
836
837         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
838                 i = oti->oti_ins_cache_size * 2;
839                 LASSERT(i < 1000);
840                 if (i == 0)
841                         i = OSD_INS_CACHE_SIZE;
842                 OBD_ALLOC(idc, sizeof(*idc) * i);
843                 if (idc == NULL)
844                         return ERR_PTR(-ENOMEM);
845                 if (oti->oti_ins_cache != NULL) {
846                         memcpy(idc, oti->oti_ins_cache,
847                                oti->oti_ins_cache_used * sizeof(*idc));
848                         OBD_FREE(oti->oti_ins_cache,
849                                  oti->oti_ins_cache_used * sizeof(*idc));
850                 }
851                 oti->oti_ins_cache = idc;
852                 oti->oti_ins_cache_size = i;
853         }
854
855         idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
856         idc->oic_fid = *fid;
857         idc->oic_dev = osd;
858         idc->oic_dnode = 0;
859         idc->oic_remote = 0;
860
861         return idc;
862 }
863
864 /**
865  * Lookup mapping for the given fid in the cache
866  *
867  * Initialize a new one if not found. the initialization checks whether
868  * the object is local or remote. for the local objects, OI is used to
869  * learn dnode#. the function is used when the caller has no information
870  * about the object, e.g. at dt_insert().
871  */
872 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
873                                              struct osd_device *osd,
874                                              const struct lu_fid *fid)
875 {
876         struct osd_idmap_cache *idc;
877         int rc;
878
879         idc = osd_idc_find(env, osd, fid);
880         if (idc != NULL)
881                 return idc;
882
883         /* new mapping is needed */
884         idc = osd_idc_add(env, osd, fid);
885         if (IS_ERR(idc))
886                 return idc;
887
888         /* initialize it */
889         rc = osd_remote_fid(env, osd, fid);
890         if (unlikely(rc < 0))
891                 return ERR_PTR(rc);
892
893         if (rc == 0) {
894                 /* the object is local, lookup in OI */
895                 uint64_t dnode;
896
897                 rc = osd_fid_lookup(env, osd, fid, &dnode);
898                 if (unlikely(rc < 0)) {
899                         CERROR("%s: can't lookup: rc = %d\n",
900                                osd->od_svname, rc);
901                         return ERR_PTR(rc);
902                 }
903                 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
904                 idc->oic_dnode = dnode;
905         } else {
906                 /* the object is remote */
907                 idc->oic_remote = 1;
908         }
909
910         return idc;
911 }
912
913 /*
914  * lookup mapping for given FID and fill it from the given object.
915  * the object is local by definition.
916  */
917 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
918                           struct osd_object *obj)
919 {
920         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
921         struct osd_idmap_cache *idc;
922
923         idc = osd_idc_find(env, osd, fid);
924         if (idc != NULL) {
925                 if (obj->oo_dn == NULL)
926                         return 0;
927                 idc->oic_dnode = obj->oo_dn->dn_object;
928                 return 0;
929         }
930
931         /* new mapping is needed */
932         idc = osd_idc_add(env, osd, fid);
933         if (IS_ERR(idc))
934                 return PTR_ERR(idc);
935
936         if (obj->oo_dn)
937                 idc->oic_dnode = obj->oo_dn->dn_object;
938
939         return 0;
940 }