Whamcloud - gitweb
e0ac33815b8d9d432790c5c2683ff8bc502dd314
[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  * Copryright (C) 2002 Cluster File Systems, Inc.
13  *
14  */
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 extern 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 struct super_block * obdfs_read_super(struct super_block *sb, 
89                                             void *data, int silent)
90 {
91         struct inode *root = 0; 
92         struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
93         struct obd_device *obddev;
94         char *device = NULL;
95         char *version = NULL;
96         int connected = 0;
97         int devno;
98         int err;
99         unsigned long blocksize;
100         unsigned long blocksize_bits;
101         unsigned long root_ino;
102         int scratch;
103         struct obdo *oa;
104         
105
106         ENTRY;
107         MOD_INC_USE_COUNT; 
108         
109         memset(sbi, 0, sizeof(*sbi));
110         
111         CDEBUG(D_INFO, "\n"); 
112         obdfs_options(data, &device, &version);
113         if ( !device ) {
114                 printk(__FUNCTION__ ": no device\n");
115                 EXIT;
116                 goto ERR;
117         }
118
119         devno = simple_strtoul(device, NULL, 0);
120         CDEBUG(D_INFO, "\n"); 
121         if ( devno >= MAX_OBD_DEVICES ) {
122                 printk(__FUNCTION__ ": device of %s too high (%d)\n", device, devno);
123                 EXIT;
124                 goto ERR;
125         } 
126
127         CDEBUG(D_INFO, "\n"); 
128         obddev = &obd_dev[devno];
129
130         CDEBUG(D_INFO, "\n"); 
131         if ( ! (obddev->obd_flags & OBD_ATTACHED) || 
132              ! (obddev->obd_flags & OBD_SET_UP) ){
133                 printk("device %s not attached or not set up (%d)\n", 
134                        device, MINOR(devno));
135                 EXIT;
136                 goto ERR;;
137         } 
138
139         CDEBUG(D_INFO, "\n"); 
140         sbi->osi_obd = obddev;
141         sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
142         
143         sbi->osi_conn.oc_dev = obddev;
144         err = obd_connect(&sbi->osi_conn);
145         if ( err ) {
146                 printk("OBDFS: cannot connect to %s\n", device);
147                 EXIT;
148                 goto ERR;
149         }
150         connected = 1;
151         CDEBUG(D_INFO, "\n"); 
152         /* list of dirty inodes, and a mutex to hold while modifying it */
153         INIT_LIST_HEAD(&sbi->osi_inodes);
154         init_MUTEX (&sbi->osi_list_mutex);
155
156         CDEBUG(D_INFO, "\n"); 
157         sbi->osi_super = sb;
158
159         CDEBUG(D_INFO, "\n"); 
160         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
161                                        "blocksize", &scratch,
162                                        (void *)&blocksize);
163         if ( err ) {
164                 printk("getinfo call to drive failed (blocksize)\n");
165                 EXIT;
166                 goto ERR;
167         }
168
169         CDEBUG(D_INFO, "\n"); 
170         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
171                                        "blocksize_bits", &scratch,
172                                        (void *)&blocksize_bits);
173         if ( err ) {
174                 printk("getinfo call to drive failed (blocksize_bits)\n");
175                 EXIT;
176                 goto ERR;
177         }
178
179         CDEBUG(D_INFO, "\n"); 
180         err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"), 
181                                        "root_ino", &scratch, (void *)&root_ino);
182         if ( err ) {
183                 printk("getinfo call to drive failed (root_ino)\n");
184                 EXIT;
185                 goto ERR;
186         }
187         
188         CDEBUG(D_INFO, "\n"); 
189         sb->s_maxbytes = 1LL << 36;
190         printk("Max bytes: %Lx\n", sb->s_maxbytes);
191         sb->s_blocksize = PAGE_SIZE;
192         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
193         sb->s_magic = OBDFS_SUPER_MAGIC;
194         sb->s_op = &obdfs_super_operations;
195
196         /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
197
198         /* make root inode */
199         CDEBUG(D_INFO, "\n"); 
200         oa = obdo_fromid(&sbi->osi_conn, root_ino, S_IFDIR,
201                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
202         CDEBUG(D_INFO, "mode %o\n", oa->o_mode); 
203         if ( IS_ERR(oa) ) {
204                 printk(__FUNCTION__ ": obdo_fromid failed\n");
205                 iput(root); 
206                 EXIT;
207                 goto ERR;
208         }
209         CDEBUG(D_INFO, "\n"); 
210         root = iget4(sb, root_ino, NULL, oa);
211         obdo_free(oa);
212         CDEBUG(D_INFO, "\n"); 
213         if (!root) {
214             printk("OBDFS: bad iget4 for root\n");
215             sb->s_dev = 0;
216             err = -ENOENT;
217             EXIT;
218             goto ERR;
219         } 
220         
221         CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
222                "minor: %d, blocksize: %ld, blocksize bits %ld\n", 
223                sb->s_dev, root->i_ino, device, MINOR(devno), 
224                blocksize, blocksize_bits);
225         sb->s_root = d_alloc_root(root);
226         list_add(&sbi->osi_list, &obdfs_super_list);
227         OBD_FREE(device, strlen(device) + 1);
228         if (version)
229                 OBD_FREE(version, strlen(version) + 1);
230         EXIT;  
231         return sb;
232
233 ERR:
234         MOD_DEC_USE_COUNT;
235         if (device)
236                 OBD_FREE(device, strlen(device) + 1);
237         if (version)
238                 OBD_FREE(version, strlen(version) + 1);
239         if (connected) 
240                 sbi->osi_ops->o_disconnect(&sbi->osi_conn);
241
242         if (sbi) {
243                 sbi->osi_super = NULL;
244         }
245         if (root) {
246                 iput(root);
247         }
248         sb->s_dev = 0;
249         return NULL;
250 } /* obdfs_read_super */
251
252
253 static void obdfs_put_super(struct super_block *sb)
254 {
255         struct obdfs_sb_info *sbi;
256
257         ENTRY;
258         sb->s_dev = 0;
259         
260         sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
261         //obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
262
263         OPS(sb,disconnect)(ID(sb));
264         list_del(&sbi->osi_list);
265         
266         printk(KERN_INFO "OBDFS: Bye bye.\n");
267
268         MOD_DEC_USE_COUNT;
269         EXIT;
270 } /* obdfs_put_super */
271
272
273 void obdfs_do_change_inode(struct inode *inode, int valid)
274 {
275         struct obdo *oa;
276         int err;
277         
278         ENTRY;
279         if (IOPS(inode, setattr) == NULL) {
280                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
281                 EXIT;
282                 return;
283         }
284         oa = obdo_alloc();
285         if ( !oa ) {
286                 printk(__FUNCTION__ ": obdo_alloc failed\n");
287                 EXIT;
288                 return;
289         }
290
291         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
292         obdfs_from_inode(oa, inode);
293         oa->o_mode = inode->i_mode;
294         err = IOPS(inode, setattr)(IID(inode), oa);
295
296         if ( err )
297                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
298
299         EXIT;
300         obdo_free(oa);
301 } /* obdfs_write_inode */
302
303 void obdfs_change_inode(struct inode *inode, int mask)
304 {
305         return obdfs_do_change_inode(inode, OBD_MD_FLNLINK); 
306 }
307
308
309 extern void write_inode_pages(struct inode *);
310 /* This routine is called from iput() (for each unlink on the inode).
311  * We can't put this call into delete_inode() since that is called only
312  * when i_count == 0, and we need to keep a reference on the inode while
313  * it is in the page cache, which means i_count > 0.  Catch 22.
314  */
315 static void obdfs_put_inode(struct inode *inode)
316 {
317         ENTRY;
318         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
319                 write_inode_pages(inode);
320                 EXIT;
321                 return;
322         }
323
324         //obdfs_dequeue_pages(inode);
325         EXIT;
326 } /* obdfs_put_inode */
327
328
329 static void obdfs_delete_inode(struct inode *inode)
330 {
331         obdfs_do_change_inode(inode, ~0);
332         clear_inode(inode); 
333 }
334 #if 0
335 {
336         struct obdo *oa;
337         int err;
338
339         ENTRY;
340         if (IOPS(inode, destroy) == NULL) {
341                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
342                 EXIT;
343                 return;
344         }
345
346         oa = obdo_alloc();
347         if ( !oa ) {
348                 printk(__FUNCTION__ ": obdo_alloc failed\n");
349                 EXIT;
350                 return;
351         }
352         oa->o_valid = OBD_MD_FLNOTOBD;
353         obdfs_from_inode(oa, inode);
354
355         /* XXX how do we know that this inode is now clean? */
356         printk("delete_inode ------> link %d\n", inode->i_nlink);
357         ODEBUG(oa);
358         err = IOPS(inode, destroy)(IID(inode), oa);
359         obdo_free(oa);
360         clear_inode(inode);
361         if (err) {
362                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
363                 EXIT;
364                 return;
365         }
366
367         EXIT;
368 } /* obdfs_delete_inode */
369 #endif
370
371
372 static int obdfs_attr2inode(struct inode * inode, struct iattr * attr)
373 {
374         unsigned int ia_valid = attr->ia_valid;
375         int error = 0;
376
377         if (ia_valid & ATTR_SIZE) {
378                 error = vmtruncate(inode, attr->ia_size);
379                 if (error)
380                         goto out;
381         }
382
383         if (ia_valid & ATTR_UID)
384                 inode->i_uid = attr->ia_uid;
385         if (ia_valid & ATTR_GID)
386                 inode->i_gid = attr->ia_gid;
387         if (ia_valid & ATTR_ATIME)
388                 inode->i_atime = attr->ia_atime;
389         if (ia_valid & ATTR_MTIME)
390                 inode->i_mtime = attr->ia_mtime;
391         if (ia_valid & ATTR_CTIME)
392                 inode->i_ctime = attr->ia_ctime;
393         if (ia_valid & ATTR_MODE) {
394                 inode->i_mode = attr->ia_mode;
395                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
396                         inode->i_mode &= ~S_ISGID;
397         }
398 out:
399         return error;
400 }
401
402 int obdfs_setattr(struct dentry *de, struct iattr *attr)
403 {
404         struct inode *inode = de->d_inode;
405         struct obdo *oa;
406         int err;
407
408         ENTRY;
409         if (IOPS(inode, setattr) == NULL) {
410                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
411                 EXIT;
412                 return -EIO;
413         }
414         oa = obdo_alloc();
415         if ( !oa ) {
416                 printk(__FUNCTION__ ": obdo_alloc failed\n");
417                 return -ENOMEM;
418         }
419
420         obdfs_attr2inode(inode, attr);
421         oa->o_id = inode->i_ino;
422         oa->o_mode = inode->i_mode;
423         obdo_from_iattr(oa, attr);
424         err = IOPS(inode, setattr)(IID(inode), oa);
425
426         if ( err )
427                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
428
429         EXIT;
430         obdo_free(oa);
431         return err;
432 } /* obdfs_setattr */
433
434
435
436 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
437 {
438         struct statfs tmp;
439         int err;
440
441         ENTRY;
442
443         err = OPS(sb,statfs)(ID(sb), &tmp);
444         if ( err ) { 
445                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
446                 return err;
447         }
448         memcpy(buf, &tmp, sizeof(*buf));
449         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
450         EXIT;
451
452         return err; 
453 }
454
455 static inline void obdfs_read_inode2(struct inode *inode, void *opaque)
456 {
457         struct obdo *oa = opaque; 
458         
459         ENTRY;
460         obdfs_to_inode(inode, oa); 
461
462         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
463         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
464
465         /* OIDEBUG(inode); */
466
467         if (S_ISREG(inode->i_mode)) {
468                 inode->i_op = &obdfs_file_inode_operations;
469                 inode->i_fop = &obdfs_file_operations;
470                 inode->i_mapping->a_ops = &obdfs_aops;
471                 EXIT;
472         } else if (S_ISDIR(inode->i_mode)) {
473                 inode->i_op = &obdfs_dir_inode_operations;
474                 inode->i_fop = &obdfs_dir_operations; 
475                 inode->i_mapping->a_ops = &obdfs_aops;
476                 EXIT;
477         } else if (S_ISLNK(inode->i_mode)) {
478                 if (inode->i_blocks) { 
479                         inode->i_op = &obdfs_symlink_inode_operations;
480                         inode->i_mapping->a_ops = &obdfs_aops;
481                 }else {
482                         inode->i_op = &obdfs_fast_symlink_inode_operations;
483                 }
484                 EXIT;
485         } else {
486                 init_special_inode(inode, inode->i_mode,
487                                    ((int *)obdfs_i2info(inode)->oi_inline)[0]);
488         }
489
490         EXIT;
491         return;
492 }
493
494 /* exported operations */
495 struct super_operations obdfs_super_operations =
496 {
497         read_inode2: obdfs_read_inode2,
498         put_inode: obdfs_put_inode,
499         delete_inode: obdfs_delete_inode,
500         put_super: obdfs_put_super,
501         statfs: obdfs_statfs
502 };
503
504
505 struct file_system_type obdfs_fs_type = {
506    "obdfs", 0, obdfs_read_super, NULL
507 };
508
509 int init_obdfs(void)
510 {
511         //int err;
512
513         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
514
515         obdfs_sysctl_init();
516
517         INIT_LIST_HEAD(&obdfs_super_list);
518         //err = obdfs_init_pgrqcache();
519         //if (err)
520         //return err;
521
522         //obdfs_flushd_init();
523         return register_filesystem(&obdfs_fs_type);
524 }
525
526
527
528
529 #ifdef MODULE
530 int init_module(void)
531 {
532         return init_obdfs();
533 }
534
535 void cleanup_module(void)
536 {
537         ENTRY;
538
539         //obdfs_flushd_cleanup();
540         obdfs_sysctl_clean();
541         //obdfs_cleanup_pgrqcache();
542         unregister_filesystem(&obdfs_fs_type);
543         CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);
544         EXIT;
545 }
546
547 #endif