Whamcloud - gitweb
This fixes a race between killing inodes in obdfs and in ext2obd.
[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         init_MUTEX (&sbi->osi_list_mutex);
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         
290         printk(KERN_INFO "OBDFS: Bye bye.\n");
291
292         MOD_DEC_USE_COUNT;
293         EXIT;
294 } /* obdfs_put_super */
295
296
297 /* all filling in of inodes postponed until lookup */
298 static void obdfs_read_inode(struct inode *inode)
299 {
300         struct obdo *oa;
301         ENTRY;
302         oa = obdo_fromid(IID(inode), inode->i_ino,
303                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
304         if ( IS_ERR(oa) ) {
305                 printk(__FUNCTION__ ": obdo_fromid failed\n");
306                 EXIT;
307                 return /* PTR_ERR(oa) */;
308         }
309
310         ODEBUG(oa);
311         obdfs_to_inode(inode, oa);
312         INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
313         INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
314
315         obdo_free(oa);
316         /* OIDEBUG(inode); */
317
318         if (S_ISREG(inode->i_mode)) {
319                 inode->i_op = &obdfs_file_inode_operations;
320                 inode->i_fop = &obdfs_file_operations;
321                 inode->i_mapping->a_ops = &obdfs_aops;
322                 EXIT;
323         } else if (S_ISDIR(inode->i_mode)) {
324                 inode->i_op = &obdfs_dir_inode_operations;
325                 inode->i_fop = &obdfs_dir_operations; 
326                 inode->i_mapping->a_ops = &obdfs_aops;
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 void obdfs_do_change_inode(struct inode *inode, int mask)
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 & (~mask);
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 void obdfs_change_inode(struct inode *inode, int mask)
374 {
375         return obdfs_do_change_inode(inode, OBD_MD_FLNLINK); 
376 }
377
378
379
380 /* This routine is called from iput() (for each unlink on the inode).
381  * We can't put this call into delete_inode() since that is called only
382  * when i_count == 0, and we need to keep a reference on the inode while
383  * it is in the page cache, which means i_count > 0.  Catch 22.
384  */
385 static void obdfs_put_inode(struct inode *inode)
386 {
387         ENTRY;
388         if (inode->i_nlink) {
389                 EXIT;
390                 return;
391         }
392
393         obdfs_dequeue_pages(inode);
394         EXIT;
395 } /* obdfs_put_inode */
396
397
398 static void obdfs_delete_inode(struct inode *inode)
399 {
400         obdfs_do_change_inode(inode, 0);
401         clear_inode(inode); 
402 }
403 #if 0
404 {
405         struct obdo *oa;
406         int err;
407
408         ENTRY;
409         if (IOPS(inode, destroy) == NULL) {
410                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
411                 EXIT;
412                 return;
413         }
414
415         oa = obdo_alloc();
416         if ( !oa ) {
417                 printk(__FUNCTION__ ": obdo_alloc failed\n");
418                 EXIT;
419                 return;
420         }
421         oa->o_valid = OBD_MD_FLNOTOBD;
422         obdfs_from_inode(oa, inode);
423
424         /* XXX how do we know that this inode is now clean? */
425         printk("delete_inode ------> link %d\n", inode->i_nlink);
426         ODEBUG(oa);
427         err = IOPS(inode, destroy)(IID(inode), oa);
428         obdo_free(oa);
429         clear_inode(inode);
430         if (err) {
431                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
432                 EXIT;
433                 return;
434         }
435
436         EXIT;
437 } /* obdfs_delete_inode */
438 #endif
439
440
441 int inode_copy_attr(struct inode * inode, struct iattr * attr)
442 {
443         unsigned int ia_valid = attr->ia_valid;
444         int error = 0;
445
446         if (ia_valid & ATTR_SIZE) {
447                 error = vmtruncate(inode, attr->ia_size);
448                 if (error)
449                         goto out;
450         }
451
452         if (ia_valid & ATTR_UID)
453                 inode->i_uid = attr->ia_uid;
454         if (ia_valid & ATTR_GID)
455                 inode->i_gid = attr->ia_gid;
456         if (ia_valid & ATTR_ATIME)
457                 inode->i_atime = attr->ia_atime;
458         if (ia_valid & ATTR_MTIME)
459                 inode->i_mtime = attr->ia_mtime;
460         if (ia_valid & ATTR_CTIME)
461                 inode->i_ctime = attr->ia_ctime;
462         if (ia_valid & ATTR_MODE) {
463                 inode->i_mode = attr->ia_mode;
464                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
465                         inode->i_mode &= ~S_ISGID;
466         }
467 out:
468         return error;
469 }
470
471 int obdfs_setattr(struct dentry *de, struct iattr *attr)
472 {
473         struct inode *inode = de->d_inode;
474         struct obdo *oa;
475         int err;
476
477         ENTRY;
478         if (IOPS(inode, setattr) == NULL) {
479                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
480                 EXIT;
481                 return -EIO;
482         }
483         oa = obdo_alloc();
484         if ( !oa ) {
485                 printk(__FUNCTION__ ": obdo_alloc failed\n");
486                 return -ENOMEM;
487         }
488
489         inode_copy_attr(inode, attr);
490         oa->o_id = inode->i_ino;
491         obdo_from_iattr(oa, attr);
492         err = IOPS(inode, setattr)(IID(inode), oa);
493
494         if ( err )
495                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
496
497         EXIT;
498         obdo_free(oa);
499         return err;
500 } /* obdfs_setattr */
501
502
503
504 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
505 {
506         struct statfs tmp;
507         int err;
508
509         ENTRY;
510
511         err = OPS(sb,statfs)(ID(sb), &tmp);
512         if ( err ) { 
513                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
514                 return err;
515         }
516         memcpy(buf, &tmp, sizeof(*buf));
517         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
518         EXIT;
519
520         return err; 
521 }
522
523 /* exported operations */
524 struct super_operations obdfs_super_operations =
525 {
526         read_inode: obdfs_read_inode,
527         put_inode: obdfs_put_inode,
528         delete_inode: obdfs_delete_inode,
529         put_super: obdfs_put_super,
530         statfs: obdfs_statfs
531 };
532
533 struct file_system_type obdfs_fs_type = {
534    "obdfs", 0, obdfs_read_super, NULL
535 };
536
537 int init_obdfs(void)
538 {
539         int err;
540
541         printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
542
543         obdfs_sysctl_init();
544
545         INIT_LIST_HEAD(&obdfs_super_list);
546         err = obdfs_init_pgrqcache();
547         if (err)
548                 return err;
549
550         obdfs_flushd_init();
551         return register_filesystem(&obdfs_fs_type);
552 }
553
554 struct address_space_operations obdfs_aops = {
555         readpage: obdfs_readpage,
556         writepage: obdfs_writepage,
557         sync_page: block_sync_page,
558         prepare_write: obdfs_prepare_write, 
559         commit_write: obdfs_commit_write,
560         bmap: NULL
561 };
562
563
564 #ifdef MODULE
565 int init_module(void)
566 {
567         return init_obdfs();
568 }
569
570 void cleanup_module(void)
571 {
572         ENTRY;
573
574         obdfs_flushd_cleanup();
575         obdfs_sysctl_clean();
576         obdfs_cleanup_pgrqcache();
577         unregister_filesystem(&obdfs_fs_type);
578         CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);
579         EXIT;
580 }
581
582 #endif