Whamcloud - gitweb
737e8d319440ad889878e1405208b7010f6e2e78
[fs/lustre-release.git] / lustre / osd-ldiskfs / 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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Whamcloud, Inc.
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_oi.c
37  *
38  * Object Index.
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 /*
44  * oi uses two mechanisms to implement fid->cookie mapping:
45  *
46  *     - persistent index, where cookie is a record and fid is a key, and
47  *
48  *     - algorithmic mapping for "igif" fids.
49  *
50  */
51
52 #define DEBUG_SUBSYSTEM S_MDS
53
54 #include <linux/module.h>
55
56 /* LUSTRE_VERSION_CODE */
57 #include <lustre_ver.h>
58 /*
59  * struct OBD_{ALLOC,FREE}*()
60  * OBD_FAIL_CHECK
61  */
62 #include <obd.h>
63 #include <obd_support.h>
64
65 /* fid_cpu_to_be() */
66 #include <lustre_fid.h>
67 #include <dt_object.h>
68
69 #include "osd_oi.h"
70 /* osd_lookup(), struct osd_thread_info */
71 #include "osd_internal.h"
72 #include "osd_igif.h"
73 #include "osd_scrub.h"
74
75 static unsigned int osd_oi_count = OSD_OI_FID_NR;
76 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
77                 "Number of Object Index containers to be created, "
78                 "it's only valid for new filesystem.");
79
80 /** to serialize concurrent OI index initialization */
81 static cfs_mutex_t oi_init_lock;
82
83 static struct dt_index_features oi_feat = {
84         .dif_flags       = DT_IND_UPDATE,
85         .dif_recsize_min = sizeof(struct osd_inode_id),
86         .dif_recsize_max = sizeof(struct osd_inode_id),
87         .dif_ptrsize     = 4
88 };
89
90 #define OSD_OI_NAME_BASE        "oi.16"
91
92 static void osd_oi_table_put(struct osd_thread_info *info,
93                              struct osd_oi **oi_table, unsigned oi_count)
94 {
95         struct iam_container *bag;
96         int                   i;
97
98         for (i = 0; i < oi_count; i++) {
99                 if (oi_table[i] == NULL)
100                         continue;
101
102                 LASSERT(oi_table[i]->oi_inode != NULL);
103
104                 bag = &(oi_table[i]->oi_dir.od_container);
105                 if (bag->ic_object == oi_table[i]->oi_inode)
106                         iam_container_fini(bag);
107                 iput(oi_table[i]->oi_inode);
108                 oi_table[i]->oi_inode = NULL;
109                 OBD_FREE_PTR(oi_table[i]);
110                 oi_table[i] = NULL;
111         }
112 }
113
114 static int osd_oi_index_create_one(struct osd_thread_info *info,
115                                    struct osd_device *osd, const char *name,
116                                    struct dt_index_features *feat)
117 {
118         const struct lu_env             *env = info->oti_env;
119         struct osd_inode_id             *id  = &info->oti_id;
120         struct buffer_head              *bh;
121         struct inode                    *inode;
122         struct ldiskfs_dir_entry_2      *de;
123         struct dentry                   *dentry;
124         struct super_block              *sb  = osd_sb(osd);
125         struct inode                    *dir = sb->s_root->d_inode;
126         handle_t                        *jh;
127         int                              rc;
128
129         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
130         bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
131         if (bh) {
132                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
133                 brelse(bh);
134                 inode = osd_iget(info, osd, id);
135                 if (!IS_ERR(inode)) {
136                         iput(inode);
137                         inode = ERR_PTR(-EEXIST);
138                 }
139                 return PTR_ERR(inode);
140         }
141
142         jh = ldiskfs_journal_start_sb(sb, 100);
143         if (IS_ERR(jh))
144                 return PTR_ERR(jh);
145
146         inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
147         if (IS_ERR(inode)) {
148                 ldiskfs_journal_stop(jh);
149                 return PTR_ERR(inode);
150         }
151
152         if (feat->dif_flags & DT_IND_VARKEY)
153                 rc = iam_lvar_create(inode, feat->dif_keysize_max,
154                                      feat->dif_ptrsize, feat->dif_recsize_max,
155                                      jh);
156         else
157                 rc = iam_lfix_create(inode, feat->dif_keysize_max,
158                                      feat->dif_ptrsize, feat->dif_recsize_max,
159                                      jh);
160         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
161         rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
162         ldiskfs_journal_stop(jh);
163         iput(inode);
164         return rc;
165 }
166
167 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
168                                        struct osd_device *osd,
169                                        const char *name,
170                                        struct dt_index_features *f,
171                                        bool create)
172 {
173         struct dentry *dentry;
174         struct inode  *inode;
175         int            rc;
176
177         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
178         if (IS_ERR(dentry))
179                 return (void *) dentry;
180
181         if (dentry->d_inode) {
182                 LASSERT(!is_bad_inode(dentry->d_inode));
183                 inode = dentry->d_inode;
184                 atomic_inc(&inode->i_count);
185                 dput(dentry);
186                 return inode;
187         }
188
189         /* create */
190         dput(dentry);
191         shrink_dcache_parent(osd_sb(osd)->s_root);
192         if (!create)
193                 return ERR_PTR(-ENOENT);
194
195         rc = osd_oi_index_create_one(info, osd, name, f);
196         if (rc)
197                 return ERR_PTR(rc);
198
199         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
200         if (IS_ERR(dentry))
201                 return (void *) dentry;
202
203         if (dentry->d_inode) {
204                 LASSERT(!is_bad_inode(dentry->d_inode));
205                 inode = dentry->d_inode;
206                 atomic_inc(&inode->i_count);
207                 dput(dentry);
208                 return inode;
209         }
210
211         return ERR_PTR(-ENOENT);
212 }
213
214 /**
215  * Open an OI(Ojbect Index) container.
216  *
217  * \param       name    Name of OI container
218  * \param       objp    Pointer of returned OI
219  *
220  * \retval      0       success
221  * \retval      -ve     failure
222  */
223 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
224                        char *name, struct osd_oi **oi_slot, bool create)
225 {
226         struct osd_directory *dir;
227         struct iam_container *bag;
228         struct inode         *inode;
229         struct osd_oi        *oi;
230         int                   rc;
231
232         ENTRY;
233
234         oi_feat.dif_keysize_min = sizeof(struct lu_fid);
235         oi_feat.dif_keysize_max = sizeof(struct lu_fid);
236
237         inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
238         if (IS_ERR(inode))
239                 RETURN(PTR_ERR(inode));
240
241         OBD_ALLOC_PTR(oi);
242         if (oi == NULL)
243                 GOTO(out_inode, rc = -ENOMEM);
244
245         oi->oi_inode = inode;
246         dir = &oi->oi_dir;
247
248         bag = &dir->od_container;
249         rc = iam_container_init(bag, &dir->od_descr, inode);
250         if (rc < 0)
251                 GOTO(out_free, rc);
252
253         rc = iam_container_setup(bag);
254         if (rc < 0)
255                 GOTO(out_container, rc);
256
257         *oi_slot = oi;
258         RETURN(0);
259
260 out_container:
261         iam_container_fini(bag);
262 out_free:
263         OBD_FREE_PTR(oi);
264 out_inode:
265         iput(inode);
266         return rc;
267 }
268
269 /**
270  * Open OI(Object Index) table.
271  * If \a oi_count is zero, which means caller doesn't know how many OIs there
272  * will be, this function can either return 0 for new filesystem, or number
273  * of OIs on existed filesystem.
274  *
275  * If \a oi_count is non-zero, which means caller does know number of OIs on
276  * filesystem, this function should return the exactly same number on
277  * success, or error code in failure.
278  *
279  * \param     oi_count  Number of expected OI containers
280  * \param     create    Create OIs if doesn't exist
281  *
282  * \retval    +ve       number of opened OI containers
283  * \retval      0       no OI containers found
284  * \retval    -ve       failure
285  */
286 static int
287 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
288                   struct osd_oi **oi_table, unsigned oi_count, bool create)
289 {
290         struct dt_device  *dev = &osd->od_dt_dev;
291         struct scrub_file *sf = &osd->od_scrub.os_file;
292         int                count = 0;
293         int                rc = 0;
294         int                i;
295         ENTRY;
296
297         /* NB: oi_count != 0 means that we have already created/known all OIs
298          * and have known exact number of OIs. */
299         LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
300
301         for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
302                 char name[12];
303
304                 if (oi_table[i] != NULL) {
305                         count++;
306                         continue;
307                 }
308
309                 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
310                 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
311                 if (rc == 0) {
312                         count++;
313                         continue;
314                 }
315
316                 if (rc == -ENOENT && create == false) {
317                         if (oi_count == 0)
318                                 return count;
319
320                         rc = 0;
321                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
322                         continue;
323                 }
324
325                 CERROR("%s: can't open %s: rc = %d\n",
326                        dev->dd_lu_dev.ld_obd->obd_name, name, rc);
327                 if (oi_count > 0)
328                         CERROR("%s: expect to open total %d OI files.\n",
329                                dev->dd_lu_dev.ld_obd->obd_name, oi_count);
330                 break;
331         }
332
333         if (rc < 0) {
334                 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
335                 count = rc;
336         }
337
338         RETURN(count);
339 }
340
341 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
342 {
343         struct dt_device  *dev = &osd->od_dt_dev;
344         struct osd_scrub  *scrub = &osd->od_scrub;
345         struct scrub_file *sf = &scrub->os_file;
346         struct osd_oi    **oi;
347         int                rc;
348         ENTRY;
349
350         OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
351         if (oi == NULL)
352                 RETURN(-ENOMEM);
353
354         cfs_mutex_lock(&oi_init_lock);
355         /* try to open existing multiple OIs first */
356         rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
357         if (rc < 0)
358                 GOTO(out, rc);
359
360         if (rc > 0) {
361                 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
362                         GOTO(out, rc);
363
364                 osd_scrub_file_reset(scrub,
365                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
366                                      SF_RECREATED);
367                 osd_oi_count = sf->sf_oi_count;
368                 goto create;
369         }
370
371         /* if previous failed then try found single OI from old filesystem */
372         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
373         if (rc == 0) { /* found single OI from old filesystem */
374                 GOTO(out, rc = 1);
375         } else if (rc != -ENOENT) {
376                 CERROR("%s: can't open %s: rc = %d\n",
377                        dev->dd_lu_dev.ld_obd->obd_name, OSD_OI_NAME_BASE, rc);
378                 GOTO(out, rc);
379         }
380
381         if (sf->sf_oi_count > 0) {
382                 int i;
383
384                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
385                 for (i = 0; i < osd_oi_count; i++)
386                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
387                 osd_scrub_file_reset(scrub,
388                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
389                                      SF_RECREATED);
390         }
391         sf->sf_oi_count = osd_oi_count;
392
393 create:
394         rc = osd_scrub_file_store(scrub);
395         if (rc < 0) {
396                 osd_oi_table_put(info, oi, sf->sf_oi_count);
397                 GOTO(out, rc);
398         }
399
400         /* No OIs exist, new filesystem, create OI objects */
401         rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
402         LASSERT(ergo(rc >= 0, rc == osd_oi_count));
403
404         GOTO(out, rc);
405
406 out:
407         if (rc < 0) {
408                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
409         } else {
410                 LASSERT((rc & (rc - 1)) == 0);
411                 osd->od_oi_table = oi;
412                 osd->od_oi_count = rc;
413                 rc = 0;
414         }
415
416         cfs_mutex_unlock(&oi_init_lock);
417         return rc;
418 }
419
420 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
421 {
422         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
423
424         OBD_FREE(osd->od_oi_table,
425                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
426         osd->od_oi_table = NULL;
427 }
428
429 static inline int fid_is_fs_root(const struct lu_fid *fid)
430 {
431         /* Map root inode to special local object FID */
432         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
433                          fid_oid(fid) == OSD_FS_ROOT_OID));
434 }
435
436 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
437                              struct osd_oi *oi, struct dt_rec *rec,
438                              const struct dt_key *key)
439 {
440         struct iam_container  *bag;
441         struct iam_iterator   *it = &oti->oti_idx_it;
442         struct iam_rec        *iam_rec;
443         struct iam_path_descr *ipd;
444         int                    rc;
445         ENTRY;
446
447         LASSERT(oi);
448         LASSERT(oi->oi_inode);
449
450         bag = &oi->oi_dir.od_container;
451         ipd = osd_idx_ipd_get(oti->oti_env, bag);
452         if (IS_ERR(ipd))
453                 RETURN(-ENOMEM);
454
455         /* got ipd now we can start iterator. */
456         iam_it_init(it, bag, 0, ipd);
457
458         rc = iam_it_get(it, (struct iam_key *)key);
459         if (rc >= 0) {
460                 if (S_ISDIR(oi->oi_inode->i_mode))
461                         iam_rec = (struct iam_rec *)oti->oti_ldp;
462                 else
463                         iam_rec = (struct iam_rec *)rec;
464
465                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
466                 if (S_ISDIR(oi->oi_inode->i_mode))
467                         osd_fid_unpack((struct lu_fid *)rec,
468                                        (struct osd_fid_pack *)iam_rec);
469         }
470         iam_it_put(it);
471         iam_it_fini(it);
472         osd_ipd_put(oti->oti_env, bag, ipd);
473
474         LINVRNT(osd_invariant(obj));
475
476         RETURN(rc);
477 }
478
479 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
480                     const struct lu_fid *fid, struct osd_inode_id *id)
481 {
482         struct lu_fid *oi_fid = &info->oti_fid2;
483         int            rc;
484
485         fid_cpu_to_be(oi_fid, fid);
486         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
487                                (const struct dt_key *)oi_fid);
488         if (rc > 0) {
489                 osd_id_unpack(id, id);
490                 rc = 0;
491         } else if (rc == 0) {
492                 rc = -ENOENT;
493         }
494         return rc;
495 }
496
497 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
498                   const struct lu_fid *fid, struct osd_inode_id *id)
499 {
500         int                  rc = 0;
501
502         if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG) {
503                 /* old OSD obj id */
504                 rc = osd_compat_objid_lookup(info, osd, fid, id);
505         } else if (fid_is_igif(fid)) {
506                 lu_igif_to_id(fid, id);
507         } else if (fid_is_fs_root(fid)) {
508                 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
509                            osd_sb(osd)->s_root->d_inode->i_generation);
510         } else {
511                 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
512                         return osd_compat_spec_lookup(info, osd, fid, id);
513
514                 rc = __osd_oi_lookup(info, osd, fid, id);
515         }
516         return rc;
517 }
518
519 static int osd_oi_iam_insert(struct osd_thread_info *oti, struct osd_oi *oi,
520                              const struct dt_rec *rec, const struct dt_key *key,
521                              struct thandle *th)
522 {
523         struct iam_container  *bag;
524         struct iam_rec        *iam_rec = (struct iam_rec *)oti->oti_ldp;
525         struct iam_path_descr *ipd;
526         struct osd_thandle    *oh;
527         int                    rc;
528 #ifdef HAVE_QUOTA_SUPPORT
529         cfs_cap_t              save    = cfs_curproc_cap_pack();
530 #endif
531         ENTRY;
532
533         LASSERT(oi);
534         LASSERT(oi->oi_inode);
535
536         bag = &oi->oi_dir.od_container;
537         ipd = osd_idx_ipd_get(oti->oti_env, bag);
538         if (unlikely(ipd == NULL))
539                 RETURN(-ENOMEM);
540
541         oh = container_of0(th, struct osd_thandle, ot_super);
542         LASSERT(oh->ot_handle != NULL);
543         LASSERT(oh->ot_handle->h_transaction != NULL);
544 #ifdef HAVE_QUOTA_SUPPORT
545         cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
546 #endif
547         if (S_ISDIR(oi->oi_inode->i_mode))
548                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec,
549                              &oti->oti_fid);
550         else
551                 iam_rec = (struct iam_rec *) rec;
552         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
553                         iam_rec, ipd);
554 #ifdef HAVE_QUOTA_SUPPORT
555         cfs_curproc_cap_unpack(save);
556 #endif
557         osd_ipd_put(oti->oti_env, bag, ipd);
558         LINVRNT(osd_invariant(obj));
559         RETURN(rc);
560 }
561
562 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
563                   const struct lu_fid *fid, const struct osd_inode_id *id,
564                   struct thandle *th)
565 {
566         struct lu_fid       *oi_fid = &info->oti_fid2;
567         struct osd_inode_id *oi_id = &info->oti_id2;
568
569         if (fid_is_igif(fid) || unlikely(fid_seq(fid) == FID_SEQ_DOT_LUSTRE))
570                 return 0;
571
572         if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
573                 return osd_compat_objid_insert(info, osd, fid, id, th);
574
575         /* Server mount should not depends on OI files */
576         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
577                 return osd_compat_spec_insert(info, osd, fid, id, th);
578
579         fid_cpu_to_be(oi_fid, fid);
580         osd_id_pack(oi_id, id);
581         return osd_oi_iam_insert(info, osd_fid2oi(osd, fid),
582                                  (const struct dt_rec *)oi_id,
583                                  (const struct dt_key *)oi_fid, th);
584 }
585
586 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
587                              const struct dt_key *key, struct thandle *handle)
588 {
589         struct iam_container  *bag;
590         struct iam_path_descr *ipd;
591         struct osd_thandle    *oh;
592         int                    rc;
593         ENTRY;
594
595         LASSERT(oi);
596
597         bag = &oi->oi_dir.od_container;
598         ipd = osd_idx_ipd_get(oti->oti_env, bag);
599         if (unlikely(ipd == NULL))
600                 RETURN(-ENOMEM);
601
602         oh = container_of0(handle, struct osd_thandle, ot_super);
603         LASSERT(oh->ot_handle != NULL);
604         LASSERT(oh->ot_handle->h_transaction != NULL);
605
606         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
607         osd_ipd_put(oti->oti_env, bag, ipd);
608         LINVRNT(osd_invariant(obj));
609         RETURN(rc);
610 }
611
612 int osd_oi_delete(struct osd_thread_info *info,
613                   struct osd_device *osd, const struct lu_fid *fid,
614                   struct thandle *th)
615 {
616         struct lu_fid *oi_fid = &info->oti_fid2;
617
618         LASSERT(fid_seq(fid) != FID_SEQ_LOCAL_FILE);
619
620         if (fid_is_idif(fid) || fid_seq(fid) == FID_SEQ_LLOG)
621                 return osd_compat_objid_delete(info, osd, fid, th);
622
623         fid_cpu_to_be(oi_fid, fid);
624         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
625                                  (const struct dt_key *)oi_fid, th);
626 }
627
628 int osd_oi_mod_init(void)
629 {
630         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
631                 osd_oi_count = OSD_OI_FID_NR;
632
633         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
634                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
635                               osd_oi_count, size_roundup_power2(osd_oi_count));
636                 osd_oi_count = size_roundup_power2(osd_oi_count);
637         }
638
639         cfs_mutex_init(&oi_init_lock);
640         return 0;
641 }