Whamcloud - gitweb
add ->{dt,md}_attr_{get,set}
[fs/lustre-release.git] / lustre / osd / osd_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/osd/osd_handler.c
5  *  Top-level entry points into osd module
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Nikita Danilov <nikita@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <linux/module.h>
35
36 /* LUSTRE_VERSION_CODE */
37 #include <linux/lustre_ver.h>
38 /*
39  * struct OBD_{ALLOC,FREE}*()
40  * OBD_FAIL_CHECK
41  */
42 #include <linux/obd_support.h>
43 /* struct ptlrpc_thread */
44 #include <linux/lustre_net.h>
45 /* LUSTRE_OSD0_NAME */
46 #include <linux/obd.h>
47 /* class_register_type(), class_unregister_type(), class_get_type() */
48 #include <linux/obd_class.h>
49 #include <linux/lustre_disk.h>
50
51 /* fid_is_local() */
52 #include <linux/lustre_fid.h>
53
54 #include "osd_internal.h"
55
56 static int   osd_root_get      (struct lu_context *ctxt,
57                                 struct dt_device *dev, struct lu_fid *f);
58 static int   osd_statfs        (struct lu_context *ctxt,
59                                 struct dt_device *dev, struct kstatfs *sfs);
60
61 static int   lu_device_is_osd  (const struct lu_device *d);
62 static void  osd_mod_exit      (void) __exit;
63 static int   osd_mod_init      (void) __init;
64 static int   osd_type_init     (struct lu_device_type *t);
65 static void  osd_type_fini     (struct lu_device_type *t);
66 static int   osd_object_init   (struct lu_context *ctxt, struct lu_object *l);
67 static void  osd_object_release(struct lu_context *ctxt, struct lu_object *l);
68 static int   osd_object_print  (struct lu_context *ctx,
69                                 struct seq_file *f, const struct lu_object *o);
70 static void  osd_device_free   (struct lu_device *m);
71 static void  osd_device_fini   (struct lu_device *d);
72 static int   osd_device_init   (struct lu_device *d, const char *conf);
73 static void *osd_key_init      (struct lu_context *ctx);
74 static void  osd_key_fini      (struct lu_context *ctx, void *data);
75 static int   osd_fid_lookup    (struct lu_context *ctx, struct osd_object *obj,
76                                 const struct lu_fid *fid);
77 static int   osd_inode_getattr (struct lu_context *ctx,
78                                 struct inode *inode, struct lu_attr *attr);
79
80 static struct osd_object  *osd_obj          (const struct lu_object *o);
81 static struct osd_device  *osd_dev          (const struct lu_device *d);
82 static struct osd_device  *osd_dt_dev       (const struct dt_device *d);
83 static struct lu_device   *osd_device_alloc (struct lu_device_type *t,
84                                              struct lustre_cfg *cfg);
85 static struct lu_object   *osd_object_alloc (struct lu_context *ctx,
86                                              struct lu_device *d);
87 static struct inode       *osd_iget         (struct osd_thread_info *info,
88                                              struct osd_device *dev,
89                                              const struct osd_inode_id *id);
90 static struct super_block *osd_sb           (const struct osd_device *dev);
91 static struct lu_fid      *osd_inode_get_fid(const struct inode *inode,
92                                              struct lu_fid *fid);
93
94 static struct lu_device_type_operations osd_device_type_ops;
95 static struct lu_device_type            osd_device_type;
96 static struct obd_ops                   osd_obd_device_ops;
97 static struct lprocfs_vars              lprocfs_osd_module_vars[];
98 static struct lprocfs_vars              lprocfs_osd_obd_vars[];
99 static struct lu_device_operations      osd_lu_ops;
100 static struct lu_context_key            osd_key;
101 static struct dt_object_operations      osd_obj_ops;
102
103 /*
104  * DT methods.
105  */
106 static int osd_root_get(struct lu_context *ctx,
107                         struct dt_device *dev, struct lu_fid *f)
108 {
109         osd_inode_get_fid(osd_dt_dev(dev)->od_root_dir->d_inode, f);
110         return 0;
111 }
112
113
114 /*
115  * OSD object methods.
116  */
117
118 static struct lu_object *osd_object_alloc(struct lu_context *ctx,
119                                           struct lu_device *d)
120 {
121         struct osd_object *mo;
122
123         OBD_ALLOC_PTR(mo);
124         if (mo != NULL) {
125                 struct lu_object *l;
126
127                 l = &mo->oo_dt.do_lu;
128                 lu_object_init(l, NULL, d);
129                 mo->oo_dt.do_ops = &osd_obj_ops;
130                 return l;
131         } else
132                 return NULL;
133 }
134
135 static int osd_object_init(struct lu_context *ctxt, struct lu_object *l)
136 {
137         return osd_fid_lookup(ctxt, osd_obj(l), lu_object_fid(l));
138 }
139
140 static void osd_object_free(struct lu_context *ctx, struct lu_object *l)
141 {
142         struct osd_object *obj = osd_obj(l);
143         OBD_FREE_PTR(obj);
144 }
145
146 static void osd_object_delete(struct lu_context *ctx, struct lu_object *l)
147 {
148         struct osd_object *o = osd_obj(l);
149
150         if (o->oo_inode != NULL)
151                 iput(o->oo_inode);
152 }
153
154 static int osd_inode_unlinked(const struct inode *inode)
155 {
156         return inode->i_nlink == !!S_ISDIR(inode->i_mode);
157 }
158
159 static void osd_object_release(struct lu_context *ctxt, struct lu_object *l)
160 {
161         struct osd_object *o = osd_obj(l);
162
163         if (o->oo_inode != NULL && osd_inode_unlinked(o->oo_inode))
164                 set_bit(LU_OBJECT_HEARD_BANSHEE, &l->lo_header->loh_flags);
165 }
166
167 static int osd_object_print(struct lu_context *ctx,
168                             struct seq_file *f, const struct lu_object *l)
169 {
170         struct osd_object  *o = osd_obj(l);
171
172         return seq_printf(f, LUSTRE_OSD0_NAME"-object@%p(i:%p:%lu/%u)",
173                           o, o->oo_inode,
174                           o->oo_inode ? o->oo_inode->i_ino : 0UL,
175                           o->oo_inode ? o->oo_inode->i_generation : 0);
176 }
177
178 static int osd_config(struct lu_context *ctx,
179                       struct dt_device *d, const char *name,
180                       void *buf, int size, int mode)
181 {
182         if (mode == LUSTRE_CONFIG_GET) {
183                 /* to be continued */
184                 return 0;
185         } else {
186                 /* to be continued */
187                 return 0;
188         }
189 }
190
191 static int osd_statfs(struct lu_context *ctx,
192                       struct dt_device *d, struct kstatfs *sfs)
193 {
194         struct osd_device *osd = dt2osd_dev(d);
195         struct super_block *sb = osd_sb(osd);
196         int result;
197
198         ENTRY;
199
200         memset(sfs, 0, sizeof(*sfs));
201         result = sb->s_op->statfs(sb, sfs);
202
203         RETURN (result);
204 }
205
206 static int osd_attr_get(struct lu_context *ctxt, struct dt_object *dt,
207                         struct lu_attr *attr)
208 {
209         return osd_inode_getattr(ctxt, dt2osd_obj(dt)->oo_inode, attr);
210 }
211
212 static struct dt_object_operations osd_obj_ops = {
213         .do_attr_get = osd_attr_get
214 };
215
216 static struct dt_device_operations osd_dt_ops = {
217         .dt_root_get = osd_root_get,
218         .dt_config   = osd_config,
219         .dt_statfs   = osd_statfs
220 };
221
222 /*
223  * OSD device type methods
224  */
225 static int osd_type_init(struct lu_device_type *t)
226 {
227         return lu_context_key_register(&osd_key);
228 }
229
230 static void osd_type_fini(struct lu_device_type *t)
231 {
232         lu_context_key_degister(&osd_key);
233 }
234
235 static struct lu_context_key osd_key = {
236         .lct_init = osd_key_init,
237         .lct_fini = osd_key_fini
238 };
239
240 static void *osd_key_init(struct lu_context *ctx)
241 {
242         struct osd_thread_info *info;
243
244         OBD_ALLOC_PTR(info);
245         if (info == NULL)
246                 info = ERR_PTR(-ENOMEM);
247         return info;
248 }
249
250 static void osd_key_fini(struct lu_context *ctx, void *data)
251 {
252         struct osd_thread_info *info = data;
253         OBD_FREE_PTR(info);
254 }
255
256 static int osd_device_init(struct lu_device *d, const char *top)
257 {
258         struct osd_device *o = osd_dev(d);
259         struct lustre_mount_info *lmi;
260         int result;
261
262         ENTRY;
263
264         lmi = server_get_mount(top);
265         if (lmi != NULL) {
266                 struct lustre_sb_info    *lsi;
267                 struct lustre_disk_data  *ldd;
268                 struct lustre_mount_data *lmd;
269
270                 /* We already mounted in lustre_fill_super */
271                 lsi = s2lsi(lmi->lmi_sb);
272                 ldd = lsi->lsi_ldd;
273                 lmd = lsi->lsi_lmd;
274
275                 CDEBUG(D_INFO, "OSD info: device=%s,\n opts=%s,\n",
276                        lmd->lmd_dev, ldd->ldd_mount_opts);
277
278                 /* save lustre_mount_info in dt_device */
279                 o->od_mount = lmi;
280                 result = osd_oi_init(&o->od_oi, osd_sb(o)->s_root,
281                                      o->od_dt_dev.dd_lu_dev.ld_site);
282                 if (result == 0) {
283                         o->od_root_dir = osd_open(osd_sb(o)->s_root,
284                                                   "ROOT", S_IFDIR);
285                         if (IS_ERR(o->od_root_dir)) {
286                                 result = PTR_ERR(o->od_root_dir);
287                                 o->od_root_dir = NULL;
288                         }
289                 }
290         } else {
291                 CERROR("Cannot get mount info for %s!\n", top);
292                 result = -EFAULT;
293         }
294         if (result != 0)
295                 osd_device_fini(d);
296         RETURN(result);
297 }
298
299 static void osd_device_fini(struct lu_device *d)
300 {
301         struct osd_device *o = osd_dev(d);
302
303         if (o->od_root_dir != NULL) {
304                 dput(o->od_root_dir);
305                 o->od_root_dir = NULL;
306         }
307         osd_oi_fini(&o->od_oi);
308         if (o->od_mount != NULL) {
309                 server_put_mount(o->od_mount->lmi_name, o->od_mount->lmi_mnt);
310                 o->od_mount = NULL;
311         }
312 }
313
314 static struct lu_device *osd_device_alloc(struct lu_device_type *t,
315                                           struct lustre_cfg *cfg)
316 {
317         struct lu_device  *l;
318         struct osd_device *o;
319
320         OBD_ALLOC_PTR(o);
321         if (o != NULL) {
322                 l = &o->od_dt_dev.dd_lu_dev;
323                 lu_device_init(&o->od_dt_dev.dd_lu_dev, t);
324                 o->od_dt_dev.dd_lu_dev.ld_ops = &osd_lu_ops;
325                 o->od_dt_dev.dd_ops = &osd_dt_ops;
326         } else
327                 l = ERR_PTR(-ENOMEM);
328         return l;
329 }
330
331 static void osd_device_free(struct lu_device *d)
332 {
333         struct osd_device *o = osd_dev(d);
334
335         lu_device_fini(d);
336         OBD_FREE_PTR(o);
337 }
338
339 /*
340  * fid<->inode<->object functions.
341  */
342
343 static struct lu_fid *osd_inode_get_fid(const struct inode *inode,
344                                         struct lu_fid *fid)
345 {
346         /*
347          * XXX: Should return fid stored together with inode in memory.
348          */
349         fid->f_seq = inode->i_ino;
350         fid->f_oid = inode->i_generation;
351         return fid;
352 }
353
354 struct dentry *osd_open(struct dentry *parent, const char *name, mode_t mode)
355 {
356         struct dentry *dentry;
357         struct dentry *result;
358
359         result = dentry = osd_lookup(parent, name);
360         if (IS_ERR(dentry)) {
361                 CERROR("Error opening %s: %ld\n", name, PTR_ERR(dentry));
362                 dentry = NULL; /* dput(NULL) below is OK */
363         } else if (dentry->d_inode == NULL) {
364                 CERROR("Not found: %s\n", name);
365                 result = ERR_PTR(-ENOENT);
366         } else if ((dentry->d_inode->i_mode & S_IFMT) != mode) {
367                 CERROR("Wrong mode: %s: %o != %o\n", name,
368                        dentry->d_inode->i_mode, mode);
369                 result = ERR_PTR(mode == S_IFDIR ? -ENOTDIR : -EISDIR);
370         }
371
372         if (IS_ERR(result))
373                 dput(dentry);
374         return result;
375 }
376
377 struct dentry *osd_lookup(struct dentry *parent, const char *name)
378 {
379         struct dentry *dentry;
380
381         CDEBUG(D_INODE, "looking up object %s\n", name);
382         down(&parent->d_inode->i_sem);
383         dentry = lookup_one_len(name, parent, strlen(name));
384         up(&parent->d_inode->i_sem);
385
386         if (IS_ERR(dentry)) {
387                 CERROR("error getting %s: %ld\n", name, PTR_ERR(dentry));
388         } else if (dentry->d_inode != NULL && is_bad_inode(dentry->d_inode)) {
389                 CERROR("got bad object %s inode %lu\n",
390                        name, dentry->d_inode->i_ino);
391                 dput(dentry);
392                 dentry = ERR_PTR(-ENOENT);
393         }
394         return dentry;
395 }
396
397 static struct inode *osd_iget(struct osd_thread_info *info,
398                               struct osd_device *dev,
399                               const struct osd_inode_id *id)
400 {
401         struct inode *inode;
402
403         inode = iget(osd_sb(dev), id->oii_ino);
404         if (inode == NULL) {
405                 CERROR("no inode\n");
406                 inode = ERR_PTR(-EACCES);
407         } else if (is_bad_inode(inode)) {
408                 CERROR("bad inode\n");
409                 iput(inode);
410                 inode = ERR_PTR(-ENOENT);
411         } else if (inode->i_generation != id->oii_gen &&
412                    id->oii_gen != OSD_GEN_IGNORE) {
413                 CERROR("stale inode\n");
414                 iput(inode);
415                 inode = ERR_PTR(-ESTALE);
416         }
417         return inode;
418
419 }
420
421 static int osd_fid_lookup(struct lu_context *ctx,
422                           struct osd_object *obj, const struct lu_fid *fid)
423 {
424         struct osd_thread_info *info;
425         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
426         struct osd_device      *dev;
427         struct osd_inode_id     id;
428         struct inode           *inode;
429         int                     result;
430
431         LASSERT(obj->oo_inode == NULL);
432         LASSERT(fid_is_sane(fid));
433         LASSERT(fid_is_local(ldev->ld_site, fid));
434
435         ENTRY;
436
437         info = lu_context_key_get(ctx, &osd_key);
438         dev  = osd_dev(ldev);
439
440         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
441                 RETURN(-ENOENT);
442
443         osd_oi_read_lock(&dev->od_oi);
444         result = osd_oi_lookup(info, &dev->od_oi, fid, &id);
445         if (result == 0) {
446                 inode = osd_iget(info, dev, &id);
447                 if (!IS_ERR(inode)) {
448                         obj->oo_inode = inode;
449                         result = 0;
450                 } else
451                         result = PTR_ERR(inode);
452         }
453         osd_oi_read_unlock(&dev->od_oi);
454         RETURN(result);
455 }
456
457 static int osd_inode_getattr(struct lu_context *ctx,
458                              struct inode *inode, struct lu_attr *attr)
459 {
460         //attr->la_atime      = inode->i_atime;
461         //attr->la_mtime      = inode->i_mtime;
462         //attr->la_ctime      = inode->i_ctime;
463         attr->la_mode       = inode->i_mode;
464         attr->la_size       = inode->i_size;
465         attr->la_blocks     = inode->i_blocks;
466         attr->la_uid        = inode->i_uid;
467         attr->la_gid        = inode->i_gid;
468         attr->la_flags      = inode->i_flags;
469         attr->la_nlink      = inode->i_nlink;
470         return 0;
471 }
472
473 /*
474  * Helpers.
475  */
476
477 static int lu_device_is_osd(const struct lu_device *d)
478 {
479         /*
480          * XXX for now. Tags in lu_device_type->ldt_something are needed.
481          */
482         return ergo(d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
483 }
484
485 static struct osd_object *osd_obj(const struct lu_object *o)
486 {
487         LASSERT(lu_device_is_osd(o->lo_dev));
488         return container_of(o, struct osd_object, oo_dt.do_lu);
489 }
490
491 static struct osd_device *osd_dt_dev(const struct dt_device *d)
492 {
493         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
494         return container_of(d, struct osd_device, od_dt_dev);
495 }
496
497 static struct osd_device *osd_dev(const struct lu_device *d)
498 {
499         LASSERT(lu_device_is_osd(d));
500         return osd_dt_dev(container_of(d, struct dt_device, dd_lu_dev));
501 }
502
503 static struct super_block *osd_sb(const struct osd_device *dev)
504 {
505         return dev->od_mount->lmi_mnt->mnt_sb;
506 }
507
508 static struct lu_device_operations osd_lu_ops = {
509         .ldo_object_alloc   = osd_object_alloc,
510         .ldo_object_init    = osd_object_init,
511         .ldo_object_free    = osd_object_free,
512         .ldo_object_release = osd_object_release,
513         .ldo_object_delete  = osd_object_delete,
514         .ldo_object_print   = osd_object_print
515 };
516
517 static struct lu_device_type_operations osd_device_type_ops = {
518         .ldto_init = osd_type_init,
519         .ldto_fini = osd_type_fini,
520
521         .ldto_device_alloc = osd_device_alloc,
522         .ldto_device_free  = osd_device_free,
523
524         .ldto_device_init    = osd_device_init,
525         .ldto_device_fini    = osd_device_fini
526 };
527
528 static struct lu_device_type osd_device_type = {
529         .ldt_tags = LU_DEVICE_DT,
530         .ldt_name = LUSTRE_OSD0_NAME,
531         .ldt_ops  = &osd_device_type_ops
532 };
533
534 /*
535  * lprocfs legacy support.
536  */
537 static struct lprocfs_vars lprocfs_osd_obd_vars[] = {
538         { 0 }
539 };
540
541 static struct lprocfs_vars lprocfs_osd_module_vars[] = {
542         { 0 }
543 };
544
545 static struct obd_ops osd_obd_device_ops = {
546         .o_owner = THIS_MODULE
547 };
548
549 LPROCFS_INIT_VARS(osd, lprocfs_osd_module_vars, lprocfs_osd_obd_vars);
550
551 static int __init osd_mod_init(void)
552 {
553         struct lprocfs_static_vars lvars;
554
555         lprocfs_init_vars(osd, &lvars);
556         return class_register_type(&osd_obd_device_ops, lvars.module_vars,
557                                    LUSTRE_OSD0_NAME, &osd_device_type);
558 }
559
560 static void __exit osd_mod_exit(void)
561 {
562         class_unregister_type(LUSTRE_OSD0_NAME);
563 }
564
565 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
566 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD0_NAME")");
567 MODULE_LICENSE("GPL");
568
569 cfs_module(osd, "0.0.2", osd_mod_init, osd_mod_exit);