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