Whamcloud - gitweb
obdfs/flushd.c: pupdated stopped on module unload.
[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 struct list_head obdfs_super_list;
36 struct super_operations obdfs_super_operations;
37 long obdfs_cache_count = 0;
38 long obd_memory = 0;
39
40 static char *obdfs_read_opt(const char *opt, char *data)
41 {
42         char *value;
43         char *retval;
44
45         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
46         if ( strncmp(opt, data, strlen(opt)) )
47                 return NULL;
48
49         if ( (value = strchr(data, '=')) == NULL )
50                 return NULL;
51
52         value++;
53         OBD_ALLOC(retval, char *, strlen(value) + 1);
54         if ( !retval ) {
55                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
56                 return NULL;
57         }
58         
59         memcpy(retval, value, strlen(value)+1);
60         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
61         return retval;
62 }
63
64 static void obdfs_options(char *options, char **dev, char **vers)
65 {
66         char *this_char;
67
68         if (!options)
69                 return;
70
71         for (this_char = strtok (options, ",");
72              this_char != NULL;
73              this_char = strtok (NULL, ",")) {
74                 CDEBUG(D_INFO, "this_char %s\n", this_char);
75                 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
76                      (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
77                         continue;
78                 
79         }
80 }
81
82 static int obdfs_getdev(char *devpath, int *dev)
83 {
84         struct dentry *dentry;
85         kdev_t devno;
86
87         dentry = lookup_dentry(devpath, NULL, 0);
88         if (IS_ERR(dentry))
89                 return PTR_ERR(dentry);
90         
91         if (!S_ISCHR(dentry->d_inode->i_mode))
92                 return -ENODEV;
93
94         devno = dentry->d_inode->i_rdev;
95         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) 
96                 return -ENODEV;
97         
98         if ( MINOR(devno) >= MAX_OBD_DEVICES ) 
99                 return -ENODEV;
100
101         *dev = devno;
102         return 0;
103 }
104
105
106 static struct super_block * obdfs_read_super(struct super_block *sb, 
107                                             void *data, int silent)
108 {
109         struct inode *root = 0; 
110         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
111         struct obd_device *obddev;
112         char *device = NULL;
113         char *version = NULL;
114         int devno;
115         int err;
116         unsigned long blocksize;
117         unsigned long blocksize_bits;
118         unsigned long root_ino;
119         int scratch;
120         
121
122         ENTRY;
123         MOD_INC_USE_COUNT; 
124         
125         memset(sbi, 0, sizeof(*sbi));
126         
127         obdfs_options(data, &device, &version);
128         if ( !device ) {
129                 printk(__FUNCTION__ ": no device\n");
130                 EXIT;
131                 goto ERR;
132         }
133
134         if ( (err = obdfs_getdev(device, &devno)) ) {
135                 printk("Cannot get devno of %s, error %d\n", device, err);
136                 EXIT;
137                 goto ERR;;
138         }
139
140         if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
141                 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
142                 EXIT;
143                 goto ERR;
144         }
145                 
146         if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
147                 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
148                        device, MINOR(devno));
149                 EXIT;
150                 goto ERR;
151         } 
152
153         obddev = &obd_dev[MINOR(devno)];
154
155         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
156              ! (obddev->obd_flags & OBD_SET_UP) ){
157                 printk("device %s not attached or not set up (%d)\n", 
158                        device, MINOR(devno));
159                 EXIT;
160                 goto ERR;;
161         } 
162
163         sbi->osi_obd = obddev;
164         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
165         
166         sbi->osi_conn.oc_dev = obddev;
167         err = sbi->osi_ops->o_connect(&sbi->osi_conn);
168         if ( err ) {
169                 printk("OBDFS: cannot connect to %s\n", device);
170                 EXIT;
171                 goto ERR;
172         }
173
174         /* list of dirty inodes, and a mutex to hold while modifying it */
175         INIT_LIST_HEAD(&sbi->osi_inodes);
176         sema_init(&sbi->osi_list_mutex, 1);
177
178         sbi->osi_super = sb;
179
180         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
181                                        "blocksize", &scratch,
182                                        (void *)&blocksize);
183         if ( err ) {
184                 printk("getinfo call to drive failed (blocksize)\n");
185                 EXIT;
186                 goto ERR;
187         }
188
189         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
190                                        "blocksize_bits", &scratch,
191                                        (void *)&blocksize_bits);
192         if ( err ) {
193                 printk("getinfo call to drive failed (blocksize_bits)\n");
194                 EXIT;
195                 goto ERR;
196         }
197
198         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
199                                        "root_ino", &scratch, (void *)&root_ino);
200         if ( err ) {
201                 printk("getinfo call to drive failed (root_ino)\n");
202                 EXIT;
203                 goto ERR;
204         }
205         
206         lock_super(sb);
207         
208         sb->s_blocksize = blocksize;
209         sb->s_blocksize_bits = (unsigned char)blocksize_bits;
210         sb->s_magic = OBDFS_SUPER_MAGIC;
211         sb->s_op = &obdfs_super_operations;
212
213         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
214
215         /* make root inode */
216         root = iget(sb, root_ino);
217         if (!root || is_bad_inode(root)) {
218             printk("OBDFS: bad iget for root\n");
219             sb->s_dev = 0;
220             err = -ENOENT;
221             unlock_super(sb);
222             EXIT;
223             goto ERR;
224         } 
225         
226         CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
227                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
228                sb->s_dev, root->i_ino, device, MINOR(devno), 
229                blocksize, blocksize_bits);
230         sb->s_root = d_alloc_root(root);
231         list_add(&sbi->osi_list, &obdfs_super_list);
232         unlock_super(sb);
233         OBD_FREE(device, strlen(device) + 1);
234         if (version)
235                 OBD_FREE(version, strlen(version) + 1);
236         EXIT;  
237         return sb;
238
239 ERR:
240         MOD_DEC_USE_COUNT;
241         if (device)
242                 OBD_FREE(device, strlen(device) + 1);
243         if (version)
244                 OBD_FREE(version, strlen(version) + 1);
245         if (sbi) {
246                 sbi->osi_super = NULL;
247         }
248         if (root) {
249                 iput(root);
250         }
251         sb->s_dev = 0;
252         return NULL;
253 } /* obdfs_read_super */
254
255
256 static void obdfs_put_super(struct super_block *sb)
257 {
258         struct obdfs_sb_info *sbi;
259
260         ENTRY;
261         sb->s_dev = 0;
262         
263         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
264         obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
265
266         OPS(sb,disconnect)(ID(sb));
267         list_del(&sbi->osi_list);
268         memset(sbi, 0, sizeof(*sbi));
269         
270         printk(KERN_INFO "OBDFS: Bye bye.\n");
271
272         MOD_DEC_USE_COUNT;
273         EXIT;
274 } /* obdfs_put_super */
275
276
277 /* all filling in of inodes postponed until lookup */
278 static void obdfs_read_inode(struct inode *inode)
279 {
280         struct obdo *oa;
281
282         ENTRY;
283         oa = obdo_fromid(IID(inode), inode->i_ino,
284                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
285         if ( IS_ERR(oa) ) {
286                 printk(__FUNCTION__ ": obdo_fromid failed\n");
287                 EXIT;
288                 return /* PTR_ERR(oa) */;
289         }
290
291         ODEBUG(oa);
292         obdfs_to_inode(inode, oa);
293         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
294         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
295
296         obdo_free(oa);
297         /* OIDEBUG(inode); */
298
299         if (S_ISREG(inode->i_mode)) {
300                 inode->i_op = &obdfs_file_inode_operations;
301                 EXIT;
302         } else if (S_ISDIR(inode->i_mode)) {
303                 inode->i_op = &obdfs_dir_inode_operations;
304                 EXIT;
305         } else if (S_ISLNK(inode->i_mode)) {
306                 inode->i_op = &obdfs_symlink_inode_operations;
307                 EXIT;
308         } else {
309                 init_special_inode(inode, inode->i_mode,
310                                    /* XXX need to fill in the ext2 side */
311                                    ((long *)obdfs_i2info(inode)->oi_inline)[0]);
312         }
313
314         return;
315 } /* obdfs_read_inode */
316
317 static void obdfs_write_inode(struct inode *inode) 
318 {
319         struct obdo *oa;
320         int err;
321         
322         ENTRY;
323         oa = obdo_alloc();
324         if ( !oa ) {
325                 printk(__FUNCTION__ ": obdo_alloc failed\n");
326                 return;
327         }
328
329         oa->o_valid = OBD_MD_FLNOTOBD;
330         obdfs_from_inode(oa, inode);
331         err = IOPS(inode, setattr)(IID(inode), oa);
332
333         if ( err ) {
334                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
335                 EXIT;
336         } else {
337                 /* Copy back attributes from oa, as there may have been
338                  * changes at the target (e.g. obdo becomes a redirector
339                  * in the snapshot layer).
340                  */
341                 obdfs_to_inode(inode, oa);
342                 EXIT;
343         }
344
345         obdo_free(oa);
346 } /* obdfs_write_inode */
347
348
349 /* Dequeue cached pages for a dying inode without writing them to disk.
350  *
351  * This routine is called from iput() (for each unlink on the inode).
352  * We can't put this code into delete_inode() since that is called only
353  * when i_count == 0, and we need to keep a reference on the inode while
354  * it is in the page cache, which means i_count > 0.  Catch 22.
355  */
356 static void obdfs_put_inode(struct inode *inode)
357 {
358         struct list_head *tmp;
359
360         ENTRY;
361         if (inode->i_nlink) {
362                 EXIT;
363                 return;
364         }
365
366         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
367         tmp = obdfs_islist(inode);
368         if ( list_empty(tmp) ) {
369                 CDEBUG(D_INFO, "no dirty pages for inode %ld\n", inode->i_ino);
370                 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
371                 EXIT;
372                 return;
373         }
374
375         /* take it out of the super list */
376         list_del(tmp);
377         INIT_LIST_HEAD(obdfs_islist(inode));
378
379         tmp = obdfs_iplist(inode);
380         while ( (tmp = tmp->prev) != obdfs_iplist(inode) ) {
381                 struct obdfs_pgrq *req;
382                 struct page *page;
383                 
384                 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
385                 page = req->rq_page;
386                 /* take it out of the list and free */
387                 obdfs_pgrq_del(req);
388                 /* now put the page away */
389                 put_page(page);
390         }
391
392         /* decrement inode reference for page cache */
393         inode->i_count--;
394
395         EXIT;
396         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
397 } /* obdfs_put_inode */
398
399
400 static void obdfs_delete_inode(struct inode *inode)
401 {
402         struct obdo *oa;
403         int err;
404         ENTRY;
405
406         oa = obdo_alloc();
407         if ( !oa ) {
408                 printk(__FUNCTION__ ": obdo_alloc failed\n");
409                 return;
410         }
411         oa->o_valid = OBD_MD_FLNOTOBD;
412         obdfs_from_inode(oa, inode);
413
414         err = IOPS(inode, destroy)(IID(inode), oa);
415         obdo_free(oa);
416
417         if (err) {
418                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
419                 EXIT;
420                 return;
421         }
422
423         EXIT;
424 } /* obdfs_delete_inode */
425
426
427 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
428 {
429         struct inode *inode = de->d_inode;
430         struct obdo *oa;
431         int err;
432
433         ENTRY;
434         oa = obdo_alloc();
435         if ( !oa ) {
436                 printk(__FUNCTION__ ": obdo_alloc failed\n");
437                 return -ENOMEM;
438         }
439
440         oa->o_id = inode->i_ino;
441         obdo_from_iattr(oa, attr);
442         err = IOPS(inode, setattr)(IID(inode), oa);
443
444         if ( err ) {
445                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
446                 EXIT;
447         } else {
448                 /* Copy back attributes from oa, as there may have been
449                  * changes at the target (e.g. obdo becomes a redirector
450                  * in the snapshot layer).
451                  */
452                 obdfs_to_inode(inode, oa);
453                 EXIT;
454         }
455
456         obdo_free(oa);
457         return err;
458 } /* obdfs_notify_change */
459
460
461 static int obdfs_statfs(struct super_block *sb, struct statfs *buf, 
462                        int bufsize)
463 {
464         struct statfs tmp;
465         int err;
466
467         ENTRY;
468
469         err = OPS(sb,statfs)(ID(sb), &tmp);
470         if ( err ) { 
471                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
472                 return err;
473         }
474         copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
475
476         EXIT;
477
478         return err; 
479 }
480
481 /* exported operations */
482 struct super_operations obdfs_super_operations =
483 {
484         obdfs_read_inode,       /* read_inode */
485         obdfs_write_inode,      /* write_inode */
486         obdfs_put_inode,        /* put_inode */
487         obdfs_delete_inode,     /* delete_inode */
488         obdfs_notify_change,    /* notify_change */
489         obdfs_put_super,        /* put_super */
490         NULL,                   /* write_super */
491         obdfs_statfs,           /* statfs */
492         NULL                    /* remount_fs */
493 };
494
495 struct file_system_type obdfs_fs_type = {
496    "obdfs", 0, obdfs_read_super, NULL
497 };
498
499 int init_obdfs(void)
500 {
501         int err;
502
503         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
504
505         obdfs_sysctl_init();
506
507         INIT_LIST_HEAD(&obdfs_super_list);
508         err = obdfs_init_pgrqcache();
509         if (err)
510                 return err;
511
512         obdfs_flushd_init();
513         return register_filesystem(&obdfs_fs_type);
514 }
515
516
517 #ifdef MODULE
518 int init_module(void)
519 {
520         return init_obdfs();
521 }
522
523 void cleanup_module(void)
524 {
525         ENTRY;
526
527         obdfs_flushd_cleanup();
528         obdfs_sysctl_clean();
529         obdfs_cleanup_pgrqcache();
530         unregister_filesystem(&obdfs_fs_type);
531         CDEBUG(D_MALLOC, "OBDFS mem used %ld, inodes %d, pages %d\n",
532                obd_memory, obd_inodes, obd_pages);
533         EXIT;
534 }
535
536 #endif