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