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