Whamcloud - gitweb
snap/*.c: get attribtutes from child connection, fixed obdo_fromid() to return
[fs/lustre-release.git] / lustre / obdfs / super.c
1 /*
2  * OBDFS Super operations
3  *
4  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
5  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
6  * Copryright (C) 1999 Seagate Technology Inc.
7  *
8  */
9
10 #define EXPORT_SYMTAB
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/stat.h>
18 #include <linux/errno.h>
19 #include <linux/locks.h>
20 #include <linux/unistd.h>
21
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
24
25 #include <linux/fs.h>
26 #include <linux/stat.h>
27 #include <asm/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <asm/segment.h>
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obdfs.h>
34
35 /* VFS super_block ops */
36 static struct super_block *obdfs_read_super(struct super_block *, void *, int);
37 static int  obdfs_notify_change(struct dentry *dentry, struct iattr *attr);
38 static void obdfs_write_inode(struct inode *);
39 static void obdfs_delete_inode(struct inode *);
40 static void obdfs_put_super(struct super_block *);
41 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
42                        int bufsiz);
43
44 /* exported operations */
45 struct super_operations obdfs_super_operations =
46 {
47         obdfs_read_inode,       /* read_inode */
48         obdfs_write_inode,      /* write_inode */
49         NULL,                   /* put_inode */
50         obdfs_delete_inode,     /* delete_inode */
51         obdfs_notify_change,    /* notify_change */
52         obdfs_put_super,        /* put_super */
53         NULL,                   /* write_super */
54         obdfs_statfs,           /* statfs */
55         NULL                    /* remount_fs */
56 };
57
58 struct list_head obdfs_super_list;
59
60 static char *obdfs_read_opt(const char *opt, char *data)
61 {
62         char *value;
63         char *retval;
64
65         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66         if ( strncmp(opt, data, strlen(opt)) )
67                 return NULL;
68
69         if ( (value = strchr(data, '=')) == NULL )
70                 return NULL;
71
72         value++;
73         OBD_ALLOC(retval, char *, strlen(value) + 1);
74         if ( !retval ) {
75                 printk("OBDFS: Out of memory!\n");
76                 return NULL;
77         }
78         
79         memcpy(retval, value, strlen(value)+1);
80         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
81         return retval;
82 }
83
84 void obdfs_options(char *options, char **dev, char **vers)
85 {
86         char *this_char;
87
88         if (!options)
89                 return;
90
91         for (this_char = strtok (options, ",");
92              this_char != NULL;
93              this_char = strtok (NULL, ",")) {
94                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
95                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
96                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
97                         continue;
98                 
99         }
100 }
101
102 static int obdfs_getdev(char *devpath, int *dev)
103 {
104         struct dentry *dentry;
105         kdev_t devno;
106
107         dentry = lookup_dentry(devpath, NULL, 0);
108         if (IS_ERR(dentry))
109                 return PTR_ERR(dentry);
110         
111         if (!S_ISCHR(dentry->d_inode->i_mode))
112                 return -ENODEV;
113
114         devno = dentry->d_inode->i_rdev;
115         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
116                 return -ENODEV;
117         
118         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
119                 return -ENODEV;
120
121         *dev = devno;
122         return 0;
123 }
124
125
126 /* XXX allocate a super_entry, and add the super to the obdfs_super_list */
127 static struct super_block * obdfs_read_super(struct super_block *sb, 
128                                             void *data, int silent)
129 {
130         struct inode *root = 0; 
131         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
132         struct obd_device *obddev;
133         char *device = NULL;
134         char *version = NULL;
135         int devno;
136         int err;
137         unsigned long blocksize;
138         unsigned long blocksize_bits;
139         unsigned long root_ino;
140         int scratch;
141         
142
143         ENTRY;
144         MOD_INC_USE_COUNT; 
145         
146         memset(sbi, 0, sizeof(*sbi));
147         
148         obdfs_options(data, &device, &version);
149         if ( !device ) {
150                 printk("No device\n");
151                 MOD_DEC_USE_COUNT;
152                 EXIT;
153                 return NULL;
154         }
155
156         if ( (err = obdfs_getdev(device, &devno)) ) {
157                 printk("Cannot get devno of %s, error %d\n", device, err);
158                 MOD_DEC_USE_COUNT;
159                 EXIT;
160                 return NULL;
161         }
162
163         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
164                 printk("Wrong major number!\n");
165                 MOD_DEC_USE_COUNT;
166                 EXIT;
167                 return NULL;
168         }
169                 
170         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
171                 printk("Minor of %s too high (%d)\n", device, MINOR(devno));
172                 MOD_DEC_USE_COUNT;
173                 EXIT;
174                 return NULL;
175         } 
176
177         obddev = &obd_dev[MINOR(devno)];
178
179         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
180              ! (obddev->obd_flags & OBD_SET_UP) ){
181                 printk("Device %s not attached or not set up (%d)\n", 
182                        device, MINOR(devno));
183                 MOD_DEC_USE_COUNT;
184                 EXIT;
185                 return NULL;
186         } 
187
188         sbi->osi_obd = obddev;
189         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
190         
191         sbi->osi_conn.oc_dev = obddev;
192         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
193         if ( err ) {
194                 printk("OBDFS: cannot connect to %s\n", device);
195                 goto ERR;
196         }
197
198         INIT_LIST_HEAD(&sbi->osi_list);
199
200         sbi->osi_super = sb;
201
202         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
203                                        "blocksize", &scratch,
204                                        (void *)&blocksize);
205         if ( err ) {
206                 printk("Getinfo call to drive failed (blocksize)\n");
207                 goto ERR;
208         }
209
210         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
211                                        "blocksize_bits", &scratch,
212                                        (void *)&blocksize_bits);
213         if ( err ) {
214                 printk("Getinfo call to drive failed (blocksize_bits)\n");
215                 goto ERR;
216         }
217
218         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
219                                        "root_ino", &scratch, (void *)&root_ino);
220         if ( err ) {
221                 printk("Getinfo call to drive failed (root_ino)\n");
222                 goto ERR;
223         }
224         
225         lock_super(sb);
226         
227         sb->s_blocksize = blocksize;
228         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
229         sb->s_magic = OBDFS_SUPER_MAGIC;
230         sb->s_op = &obdfs_super_operations;
231
232         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
233
234         /* make root inode */
235         root = iget(sb, root_ino);
236         if (!root || is_bad_inode(root)) {
237             printk("OBDFS: bad iget for root\n");
238             sb->s_dev = 0;
239             err = -ENOENT;
240             unlock_super(sb);
241             goto ERR;
242         } 
243         
244         printk("obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
245                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
246                sb->s_dev, root->i_ino, device, MINOR(devno), 
247                blocksize, blocksize_bits);
248         sb->s_root = d_alloc_root(root);
249         unlock_super(sb);
250         EXIT;  
251         return sb;
252
253 ERR:
254         EXIT;  
255         MOD_DEC_USE_COUNT;
256         if (sbi) {
257                 sbi->osi_super = NULL;
258         }
259         if (root) {
260                 iput(root);
261         }
262         sb->s_dev = 0;
263         return NULL;
264 }
265
266 /* XXX remove the super to the obdfs_super_list */
267 static void obdfs_put_super(struct super_block *sb)
268 {
269         struct obdfs_sb_info *sbi;
270
271         ENTRY;
272
273
274         sb->s_dev = 0;
275         
276         /* XXX flush stuff */
277         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
278
279         OPS(sb,disconnect)(ID(sb));
280
281         memset(sbi, 0, sizeof(* sbi));
282         
283         printk("OBDFS: Bye bye.\n");
284
285         MOD_DEC_USE_COUNT;
286         EXIT;
287 } /* obdfs_put_super */
288
289 void inline obdfs_from_inode(struct obdo *oa, struct inode *inode)
290 {
291         struct obdfs_inode_info *oinfo = OBDFS_INFO(inode);
292
293         CDEBUG(D_INODE, "inode %ld (%p)\n", inode->i_ino, inode);
294         obdo_from_inode(oa, inode);
295         if (obdfs_has_inline(inode)) {
296                 CDEBUG(D_INODE, "inode has inline data\n");
297                 memcpy(oa->o_inline, oinfo->oi_inline, OBD_INLINESZ);
298                 oa->o_obdflags |= OBD_FL_INLINEDATA;
299                 oa->o_valid |= OBD_MD_FLINLINE;
300         }
301         if (obdfs_has_obdmd(inode)) {
302                 CDEBUG(D_INODE, "inode %ld has obdmd data\n");
303                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
304         }
305 } /* obdfs_from_inode */
306
307 void inline obdfs_to_inode(struct inode *inode, struct obdo *oa)
308 {
309         struct obdfs_inode_info *oinfo = OBDFS_INFO(inode);
310
311         CDEBUG(D_INODE, "inode %ld (%p)\n", inode->i_ino, inode);
312         obdo_to_inode(inode, oa);
313         if (obdo_has_inline(oa)) {
314                 CDEBUG(D_INODE, "obdo has inline data\n");
315                 memcpy(oinfo->oi_inline, oa->o_inline, OBD_INLINESZ);
316                 oinfo->oi_flags |= OBD_FL_INLINEDATA;
317         }
318         if (obdo_has_obdmd(oa)) {
319                 CDEBUG(D_INODE, "obdo has obdmd data\n");
320                 oinfo->oi_flags |= OBD_FL_OBDMDEXISTS;
321         }
322 } /* obdfs_to_inode */
323
324 /* all filling in of inodes postponed until lookup */
325 void obdfs_read_inode(struct inode *inode)
326 {
327         struct obdo *oa;
328         int err;
329
330         ENTRY;
331         oa = obdo_alloc();
332         if (!oa) {
333                 printk("obdfs_read_inode: obdo_alloc failed\n");
334                 EXIT;
335                 return;
336         }
337         oa->o_valid = ~OBD_MD_FLOBDMD;
338         oa->o_id = inode->i_ino;
339
340         INIT_LIST_HEAD(&OBDFS_INFO(inode)->oi_pages);
341         
342         err = IOPS(inode, getattr)(IID(inode), oa);
343         if ( err ) {
344                 printk("obdfs_read_inode: obd_getattr fails (%d)\n", err);
345                 obdo_free(oa);
346                 EXIT;
347                 return;
348         }
349
350         ODEBUG(oa);
351         obdfs_to_inode(inode, oa);
352         INIT_LIST_HEAD(&OBDFS_LIST(inode));
353
354         obdo_free(oa);
355         OIDEBUG(inode);
356
357         if (S_ISREG(inode->i_mode))
358                 inode->i_op = &obdfs_file_inode_operations;
359         else if (S_ISDIR(inode->i_mode))
360                 inode->i_op = &obdfs_dir_inode_operations;
361         else if (S_ISLNK(inode->i_mode))
362                 inode->i_op = &obdfs_symlink_inode_operations;
363         else
364                 init_special_inode(inode, inode->i_mode,
365                                    ((long *)OBDFS_INFO(inode)->oi_inline)[0]);
366
367         EXIT;
368         return;
369 } /* obdfs_read_inode */
370
371 static void obdfs_write_inode(struct inode *inode) 
372 {
373         struct obdo *oa;
374         int err;
375         
376         ENTRY;
377         oa = obdo_alloc();
378         if ( !oa ) {
379                 printk("obdfs_write_inode: obdo_alloc failed\n");
380                 return;
381         }
382
383         oa->o_valid = ~OBD_MD_FLOBDMD;
384         obdfs_from_inode(oa, inode);
385         err = IOPS(inode, setattr)(IID(inode), oa);
386
387         if ( err ) {
388                 printk("obdfs_write_inode: obd_setattr fails (%d)\n", err);
389                 EXIT;
390         } else {
391                 /* Copy back attributes from oa, as there may have been
392                  * changes at the target (e.g. obdo becomes a redirector
393                  * in the snapshot layer).
394                  */
395                 obdfs_to_inode(inode, oa);
396                 EXIT;
397         }
398
399         obdo_free(oa);
400 } /* obdfs_write_inode */
401
402
403 static void obdfs_delete_inode(struct inode *inode)
404 {
405         struct obdo *oa;
406         int err;
407         ENTRY;
408
409         oa = obdo_alloc();
410         oa->o_valid = ~OBD_MD_FLOBDMD;
411         obdfs_from_inode(oa, inode);
412
413         err = IOPS(inode, destroy)(IID(inode), oa);
414         obdo_free(oa);
415
416         if (err) {
417                 printk("obdfs_delete_node: obd_destroy fails (%d)\n", err);
418                 EXIT;
419                 return;
420         }
421
422         EXIT;
423 } /* obdfs_delete_inode */
424
425
426 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
427 {
428         struct inode *inode = de->d_inode;
429         struct obdo *oa;
430         int err;
431
432         ENTRY;
433         oa = obdo_alloc();
434         if ( !oa ) {
435                 printk("obdfs_notify_change: obdo_alloc failed\n");
436                 return -ENOMEM;
437         }
438
439         oa->o_id = inode->i_ino;
440         obdo_from_iattr(oa, attr);
441         err = IOPS(inode, setattr)(IID(inode), oa);
442
443         if ( err ) {
444                 printk("obdfs_notify_change: obd_setattr fails (%d)\n", err);
445                 EXIT;
446         } else {
447                 /* Copy back attributes from oa, as there may have been
448                  * changes at the target (e.g. obdo becomes a redirector
449                  * in the snapshot layer).
450                  */
451                 obdfs_to_inode(inode, oa);
452                 EXIT;
453         }
454
455         obdo_free(oa);
456         return err;
457 } /* obdfs_notify_change */
458
459
460 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
461                        int bufsize)
462 {
463         struct statfs tmp;
464         int err;
465
466         ENTRY;
467
468         err = OPS(sb,statfs)(ID(sb), &tmp);
469         if ( err ) { 
470                 printk("obdfs_notify_change: obd_statfs fails (%d)\n", err);
471                 return err;
472         }
473         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
474
475         EXIT;
476
477         return err; 
478 }
479
480 struct file_system_type obdfs_fs_type = {
481    "obdfs", 0, obdfs_read_super, NULL
482 };
483
484 int init_obdfs(void)
485 {
486         int err;
487
488         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
489
490         obdfs_sysctl_init();
491
492         INIT_LIST_HEAD(&obdfs_super_list);
493         err = obdfs_init_pgrqcache();
494         if (err)
495                 return err;
496
497         flushd_init();
498         return register_filesystem(&obdfs_fs_type);
499 }
500
501
502 #ifdef MODULE
503 int init_module(void)
504 {
505         return init_obdfs();
506 }
507
508 void cleanup_module(void)
509 {
510         ENTRY;
511
512         obdfs_sysctl_clean();
513         obdfs_cleanup_pgrqcache();
514         unregister_filesystem(&obdfs_fs_type);
515
516         EXIT;
517 }
518
519 #endif