Whamcloud - gitweb
LU-2558 test: recovery-small 19a FAIL no eviction
[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.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * Copyright (c) 2012, Intel Corporation.
32  * Use is subject to license terms.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/osd-zfs/osd_oi.c
39  * OI functions to map fid to dnode
40  *
41  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42  * Author: Mike Pershin <tappro@whamcloud.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_OSD
49
50 #include <lustre_ver.h>
51 #include <libcfs/libcfs.h>
52 #include <obd_support.h>
53 #include <lustre_net.h>
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58
59 #include "osd_internal.h"
60
61 #include <sys/dnode.h>
62 #include <sys/dbuf.h>
63 #include <sys/spa.h>
64 #include <sys/stat.h>
65 #include <sys/zap.h>
66 #include <sys/spa_impl.h>
67 #include <sys/zfs_znode.h>
68 #include <sys/dmu_tx.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/dsl_prop.h>
71 #include <sys/sa_impl.h>
72 #include <sys/txg.h>
73
74 static char *oi_tag = "osd_mount, oi";
75
76 #define OSD_OI_FID_NR         (1UL << 7)
77 #define OSD_OI_FID_NR_MAX     (1UL << OSD_OI_FID_OID_BITS_MAX)
78 unsigned int osd_oi_count = OSD_OI_FID_NR;
79
80
81 /*
82  * zfs osd maintains names for known fids in the name hierarchy
83  * so that one can mount filesystem with regular ZFS stack and
84  * access files
85  */
86 struct named_oid {
87         unsigned long    oid;
88         char            *name;
89 };
90
91 static const struct named_oid oids[] = {
92         { OFD_LAST_RECV_OID,            LAST_RCVD },
93         { OFD_LAST_GROUP_OID,           "LAST_GROUP" },
94         { LLOG_CATALOGS_OID,            "CATALOGS" },
95         { MGS_CONFIGS_OID,              NULL /*MOUNT_CONFIGS_DIR*/ },
96         { FID_SEQ_SRV_OID,              "seq_srv" },
97         { FID_SEQ_CTL_OID,              "seq_ctl" },
98         { MDD_CAPA_KEYS_OID,            NULL /*CAPA_KEYS*/ },
99         { FLD_INDEX_OID,                "fld" },
100         { MDD_LOV_OBJ_OID,              LOV_OBJID },
101         { MDT_LAST_RECV_OID,            LAST_RCVD },
102         { OFD_HEALTH_CHECK_OID,         HEALTH_CHECK },
103         { ACCT_USER_OID,                "acct_usr_inode" },
104         { ACCT_GROUP_OID,               "acct_grp_inode" },
105         { MDD_ROOT_INDEX_OID,           NULL },
106         { MDD_ORPHAN_OID,               NULL },
107         { 0,                            NULL }
108 };
109
110 static char *oid2name(const unsigned long oid)
111 {
112         int i = 0;
113
114         while (oids[i].oid) {
115                 if (oids[i].oid == oid)
116                         return oids[i].name;
117                 i++;
118         }
119         return NULL;
120 }
121
122 /*
123  * objects w/o a natural reference (unlike a file on a MDS)
124  * are put under a special hierarchy /O/<seq>/d0..dXX
125  * this function returns a directory specific fid belongs to
126  */
127 static uint64_t
128 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
129                         const struct lu_fid *fid, char *buf)
130 {
131         unsigned long   b;
132         int             rc;
133
134         rc = fid_ostid_pack(fid, &osd_oti_get(env)->oti_ostid);
135         LASSERT(rc == 0); /* we should not get here with IGIF */
136         b = osd_oti_get(env)->oti_ostid.oi_id % OSD_OST_MAP_SIZE;
137         LASSERT(osd->od_ost_compat_dirs[b]);
138
139         sprintf(buf, LPU64, osd_oti_get(env)->oti_ostid.oi_id);
140
141         return osd->od_ost_compat_dirs[b];
142 }
143
144 /* XXX: f_ver is not counted, but may differ too */
145 static void osd_fid2str(char *buf, const struct lu_fid *fid)
146 {
147         sprintf(buf, DFID_NOBRACE, PFID(fid));
148 }
149
150 /*
151  * Determine the zap object id which is being used as the OI for the
152  * given fid.  The lowest N bits in the sequence ID are used as the
153  * index key.  On failure 0 is returned which zfs treats internally
154  * as an invalid object id.
155  */
156 static uint64_t
157 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
158                     char *buf)
159 {
160         struct osd_oi *oi;
161
162         LASSERT(osd->od_oi_table != NULL);
163         oi = osd->od_oi_table[fid_seq(fid) & (osd->od_oi_count - 1)];
164         osd_fid2str(buf, fid);
165
166         return oi->oi_zapid;
167 }
168
169 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
170                             const struct lu_fid *fid, char *buf)
171 {
172         uint64_t zapid;
173
174         LASSERT(fid);
175         LASSERT(buf);
176
177         if (fid_is_idif(fid)) {
178                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf);
179         } else if (fid_is_last_id(fid)) {
180                 zapid = osd->od_ost_compat_grp0;
181         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
182                 /* special objects with fixed known fids get their name */
183                 char *name = oid2name(fid_oid(fid));
184
185                 if (name) {
186                         zapid = osd->od_root;
187                         strcpy(buf, name);
188                         if (fid_is_acct(fid))
189                                 zapid = MASTER_NODE_OBJ;
190                 } else {
191                         zapid = osd_get_idx_for_fid(osd, fid, buf);
192                 }
193         } else {
194                 zapid = osd_get_idx_for_fid(osd, fid, buf);
195         }
196
197         return zapid;
198 }
199
200 static inline int fid_is_fs_root(const struct lu_fid *fid)
201 {
202         /* Map root inode to special local object FID */
203         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
204                 fid_oid(fid) == OSD_FS_ROOT_OID;
205 }
206
207 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
208                    const struct lu_fid *fid, uint64_t *oid)
209 {
210         struct osd_thread_info  *info = osd_oti_get(env);
211         char                    *buf = info->oti_buf;
212         uint64_t                zapid;
213         int                     rc = 0;
214         ENTRY;
215
216         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
217                 RETURN(-ENOENT);
218
219         if (unlikely(fid_is_acct(fid))) {
220                 if (fid_oid(fid) == ACCT_USER_OID)
221                         *oid = dev->od_iusr_oid;
222                 else
223                         *oid = dev->od_igrp_oid;
224         } else if (unlikely(fid_is_fs_root(fid))) {
225                 *oid = dev->od_root;
226         } else {
227                 zapid = osd_get_name_n_idx(env, dev, fid, buf);
228
229                 rc = -zap_lookup(dev->od_objset.os, zapid, buf,
230                                 8, 1, &info->oti_zde);
231                 if (rc)
232                         RETURN(rc);
233                 *oid = info->oti_zde.lzd_reg.zde_dnode;
234         }
235
236         RETURN(rc);
237 }
238
239 /**
240  * Lookup an existing OI by the given name.
241  */
242 static int
243 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
244               uint64_t parent, const char *name, struct osd_oi *oi)
245 {
246         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
247         int                      rc;
248
249         rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
250         if (rc)
251                 return rc;
252
253         strncpy(oi->oi_name, name, OSD_OI_NAME_SIZE - 1);
254         oi->oi_zapid = zde->zde_dnode;
255
256         return rc;
257 }
258
259 /**
260  * Create a new OI with the given name.
261  */
262 static int
263 osd_oi_create(const struct lu_env *env, struct osd_device *o,
264               uint64_t parent, const char *name, uint64_t *child)
265 {
266         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
267         struct lu_attr          *la = &osd_oti_get(env)->oti_la;
268         dmu_buf_t               *db;
269         dmu_tx_t                *tx;
270         int                      rc;
271
272         /* verify it doesn't already exist */
273         rc = -zap_lookup(o->od_objset.os, parent, name, 8, 1, (void *)zde);
274         if (rc == 0)
275                 return -EEXIST;
276
277         /* create fid-to-dnode index */
278         tx = dmu_tx_create(o->od_objset.os);
279         if (tx == NULL)
280                 return -ENOMEM;
281
282         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1, NULL);
283         dmu_tx_hold_bonus(tx, parent);
284         dmu_tx_hold_zap(tx, parent, TRUE, name);
285         LASSERT(tx->tx_objset->os_sa);
286         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
287
288         rc = -dmu_tx_assign(tx, TXG_WAIT);
289         if (rc) {
290                 dmu_tx_abort(tx);
291                 return rc;
292         }
293
294         la->la_valid = LA_MODE | LA_UID | LA_GID;
295         la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
296         la->la_uid = la->la_gid = 0;
297         __osd_zap_create(env, &o->od_objset, &db, tx, la, oi_tag, 0);
298
299         zde->zde_dnode = db->db_object;
300         zde->zde_pad = 0;
301         zde->zde_type = IFTODT(S_IFDIR);
302
303         rc = -zap_add(o->od_objset.os, parent, name, 8, 1, (void *)zde, tx);
304
305         dmu_tx_commit(tx);
306
307         *child = db->db_object;
308         sa_buf_rele(db, oi_tag);
309
310         return rc;
311 }
312
313 static int
314 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
315                       uint64_t parent, const char *name, uint64_t *child)
316 {
317         struct osd_oi   oi;
318         int             rc;
319
320         rc = osd_oi_lookup(env, o, parent, name, &oi);
321         if (rc == 0) {
322                 *child = oi.oi_zapid;
323         } else if (rc == -ENOENT) {
324                 rc = osd_oi_create(env, o, parent, name, child);
325         }
326
327         return rc;
328 }
329
330 /**
331  * Close an entry in a specific slot.
332  */
333 static void
334 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
335 {
336         struct osd_oi *oi;
337
338         LASSERT(key < o->od_oi_count);
339
340         oi = o->od_oi_table[key];
341         if (oi) {
342                 OBD_FREE_PTR(oi);
343                 o->od_oi_table[key] = NULL;
344         }
345 }
346
347 /**
348  * Allocate and open a new entry in the specified unused slot.
349  */
350 static int
351 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
352                  char *name, int key)
353 {
354         struct osd_oi *oi;
355         int rc;
356
357         LASSERT(key < o->od_oi_count);
358         LASSERT(o->od_oi_table[key] == NULL);
359
360         OBD_ALLOC_PTR(oi);
361         if (oi == NULL)
362                 return -ENOMEM;
363
364         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
365         if (rc) {
366                 OBD_FREE_PTR(oi);
367                 return rc;
368         }
369
370         o->od_oi_table[key] = oi;
371
372         return 0;
373 }
374
375 /**
376  * Depopulate the OI table.
377  */
378 static void
379 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
380 {
381         int i;
382
383         for (i = 0; i < o->od_oi_count; i++)
384                 osd_oi_remove_table(env, o, i);
385 }
386
387 /**
388  * Populate the OI table based.
389  */
390 static int
391 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
392 {
393         char name[16];
394         int  i, rc = 0;
395         ENTRY;
396
397         for (i = 0; i < count; i++) {
398                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
399                 rc = osd_oi_add_table(env, o, name, i);
400                 if (rc) {
401                         osd_oi_close_table(env, o);
402                         break;
403                 }
404         }
405
406         RETURN(rc);
407 }
408
409 /**
410  * Determine if the type and number of OIs used by this file system.
411  */
412 static int
413 osd_oi_probe(const struct lu_env *env, struct osd_device *o, int *count)
414 {
415         uint64_t        root_oid = o->od_root;
416         struct osd_oi   oi;
417         char            name[16];
418         int             rc;
419         ENTRY;
420
421         /*
422          * Check for multiple OIs and determine the count.  There is no
423          * gap handling, if an OI is missing the wrong size can be returned.
424          * The only safeguard is that we know the number of OIs must be a
425          * power of two and this is checked for basic sanity.
426          */
427         for (*count = 0; *count < OSD_OI_FID_NR_MAX; (*count)++) {
428                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, *count);
429                 rc = osd_oi_lookup(env, o, root_oid, name, &oi);
430                 if (rc == 0)
431                         continue;
432
433                 if (rc == -ENOENT) {
434                         if (*count == 0)
435                                 break;
436
437                         if ((*count & (*count - 1)) != 0)
438                                 RETURN(-EDOM);
439
440                         RETURN(0);
441                 }
442
443                 RETURN(rc);
444         }
445
446         /*
447          * No OIs exist, this must be a new filesystem.
448          */
449         *count = 0;
450
451         RETURN(0);
452 }
453
454 /**
455  * Create /O subdirectory to map legacy OST objects for compatibility.
456  */
457 static int
458 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
459 {
460         char            *key = osd_oti_get(env)->oti_buf;
461         uint64_t         odb, sdb;
462         int              i, rc;
463         ENTRY;
464
465         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
466         if (rc)
467                 RETURN(rc);
468
469         /* create /O/0 subdirectory to map legacy OST objects */
470         rc = osd_oi_find_or_create(env, o, sdb, "0", &odb);
471         if (rc)
472                 RETURN(rc);
473
474         o->od_ost_compat_grp0 = odb;
475
476         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
477                 sprintf(key, "d%d", i);
478                 rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
479                 if (rc)
480                         RETURN(rc);
481
482                 o->od_ost_compat_dirs[i] = sdb;
483         }
484
485         /* Create on-disk indexes to maintain per-UID/GID inode usage.
486          * Those new indexes are created in the top-level ZAP outside the
487          * namespace in order not to confuse ZPL which might interpret those
488          * indexes as directories and assume the values are object IDs */
489         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
490                         oid2name(ACCT_USER_OID), &odb);
491         if (rc)
492                 RETURN(rc);
493         o->od_iusr_oid = odb;
494
495         rc = osd_oi_find_or_create(env, o, MASTER_NODE_OBJ,
496                         oid2name(ACCT_GROUP_OID), &odb);
497         if (rc)
498                 RETURN(rc);
499         o->od_igrp_oid = odb;
500
501         RETURN(rc);
502 }
503
504 /**
505  * Initialize the OIs by either opening or creating them as needed.
506  */
507 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
508 {
509         char    *key = osd_oti_get(env)->oti_buf;
510         int      i, rc, count = 0;
511         ENTRY;
512
513         rc = osd_oi_probe(env, o, &count);
514         if (rc)
515                 RETURN(rc);
516
517         if (count == 0) {
518                 uint64_t odb, sdb;
519
520                 count = osd_oi_count;
521                 odb = o->od_root;
522
523                 for (i = 0; i < count; i++) {
524                         sprintf(key, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
525                         rc = osd_oi_find_or_create(env, o, odb, key, &sdb);
526                         if (rc)
527                                 RETURN(rc);
528                 }
529         }
530
531         rc = osd_oi_init_compat(env, o);
532         if (rc)
533                 RETURN(rc);
534
535         LASSERT((count & (count - 1)) == 0);
536         o->od_oi_count = count;
537         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
538         if (o->od_oi_table == NULL)
539                 RETURN(-ENOMEM);
540
541         rc = osd_oi_open_table(env, o, count);
542         if (rc) {
543                 OBD_FREE(o->od_oi_table, sizeof(struct osd_oi *) * count);
544                 o->od_oi_table = NULL;
545         }
546
547         RETURN(rc);
548 }
549
550 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
551 {
552         ENTRY;
553
554         if (o->od_oi_table != NULL) {
555                 (void) osd_oi_close_table(env, o);
556                 OBD_FREE(o->od_oi_table,
557                          sizeof(struct osd_oi *) * o->od_oi_count);
558                 o->od_oi_table = NULL;
559                 o->od_oi_count = 0;
560         }
561
562         EXIT;
563 }
564
565 int osd_options_init(void)
566 {
567         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
568         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
569                 osd_oi_count = OSD_OI_FID_NR;
570
571         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
572                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
573                         osd_oi_count, size_roundup_power2(osd_oi_count));
574                 osd_oi_count = size_roundup_power2(osd_oi_count);
575         }
576
577         return 0;
578 }
579
580