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