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