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