Whamcloud - gitweb
Fix a broken macro.
[fs/lustre-release.git] / lustre / llite / super.c
1 /*
2  * Lustre Light 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  * Copryright (C) 2002 Cluster File Systems, 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/lustre_light.h>
37
38 //struct list_head ll_super_list;
39 extern struct address_space_operations ll_aops;
40 struct super_operations ll_super_operations;
41 long ll_cache_count = 0;
42 long ll_mutex_start = 0;
43 long obd_memory = 0;
44
45 static char *ll_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 ll_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 = ll_read_opt("device", this_char)))||
81                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
82                         continue;
83                 
84         }
85 }
86
87 static struct super_block * ll_read_super(struct super_block *sb, 
88                                             void *data, int silent)
89 {
90         struct inode *root = 0; 
91         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
92         struct obd_device *obddev;
93         char *device = NULL;
94         char *version = NULL;
95         int root_ino = 2;
96         int connected = 0;
97         int devno;
98         int err;
99         struct mds_rep *rep; 
100         struct mds_rep_hdr *hdr = NULL; 
101         
102
103         ENTRY;
104         MOD_INC_USE_COUNT; 
105         memset(sbi, 0, sizeof(*sbi));
106         
107         CDEBUG(D_INFO, "\n"); 
108         ll_options(data, &device, &version);
109         if ( !device ) {
110                 printk(__FUNCTION__ ": no device\n");
111                 EXIT;
112                 goto ERR;
113         }
114
115         devno = simple_strtoul(device, NULL, 0);
116         CDEBUG(D_INFO, "\n"); 
117         if ( devno >= MAX_OBD_DEVICES ) {
118                 printk(__FUNCTION__ ": device of %s too high (%d)\n", device, devno);
119                 EXIT;
120                 goto ERR;
121         } 
122
123         CDEBUG(D_INFO, "\n"); 
124
125         obddev = &obd_dev[devno];
126         sbi->ll_obd = obddev;
127         sbi->ll_ops = sbi->ll_obd->obd_type->typ_ops;
128         sbi->ll_conn.oc_dev = obddev;
129         CDEBUG(D_INFO, "\n"); 
130
131         err = obd_connect(&sbi->ll_conn);
132         CDEBUG(D_INFO, "\n"); 
133         if ( err ) {
134                 printk("OBDFS: cannot connect to %s\n", device);
135                 EXIT;
136                 goto ERR;
137         }
138         connected = 1;
139
140         CDEBUG(D_INFO, "\n"); 
141         /* list of dirty inodes, and a mutex to hold while modifying it */
142         INIT_LIST_HEAD(&sbi->ll_inodes);
143         init_MUTEX (&sbi->ll_list_mutex);
144
145         CDEBUG(D_INFO, "\n"); 
146         sbi->ll_super = sb;
147         sbi->ll_rootino = 2;
148         
149         CDEBUG(D_INFO, "\n"); 
150         sb->s_maxbytes = 1LL << 36;
151         printk("Max bytes: %Lx\n", sb->s_maxbytes);
152         sb->s_blocksize = PAGE_SIZE;
153         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
154         sb->s_magic = LL_SUPER_MAGIC;
155         sb->s_op = &ll_super_operations;
156
157         /* make root inode */
158         CDEBUG(D_INFO, "\n"); 
159         err = mdc_getattr(root_ino, S_IFDIR, OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 
160                          &rep, &hdr);
161         if (err) {
162                 printk(__FUNCTION__ ": mds_getattr failed %d\n", err);
163                 iput(root); 
164                 if (rep)
165                         kfree(rep);
166                 EXIT;
167                 goto ERR;
168         }
169                          
170         CDEBUG(D_INFO, "mode %o\n", rep->mode);
171         CDEBUG(D_INFO, "\n"); 
172         root = iget4(sb, root_ino, NULL, rep);
173         kfree(hdr);
174         CDEBUG(D_INFO, "\n"); 
175         if (!root) {
176             printk("OBDFS: bad iget4 for root\n");
177             sb->s_dev = 0;
178             err = -ENOENT;
179             EXIT;
180             goto ERR;
181         } 
182         
183         sb->s_root = d_alloc_root(root);
184         //        list_add(&sbi->ll_list, &ll_super_list);
185         OBD_FREE(device, strlen(device) + 1);
186         if (version)
187                 OBD_FREE(version, strlen(version) + 1);
188         EXIT;  
189         return sb;
190
191 ERR:
192         MOD_DEC_USE_COUNT;
193         if (device)
194                 OBD_FREE(device, strlen(device) + 1);
195         if (version)
196                 OBD_FREE(version, strlen(version) + 1);
197         if (connected) 
198                 sbi->ll_ops->o_disconnect(&sbi->ll_conn);
199
200         if (sbi) {
201                 sbi->ll_super = NULL;
202         }
203         if (root) {
204                 iput(root);
205         }
206         sb->s_dev = 0;
207         return NULL;
208 } /* ll_read_super */
209
210
211 static void ll_put_super(struct super_block *sb)
212 {
213         struct ll_sb_info *sbi;
214
215         ENTRY;
216         sb->s_dev = 0;
217         
218         sbi = (struct ll_sb_info *) &sb->u.generic_sbp;
219         //ll_flush_reqs(&sbi->ll_inodes, ~0UL);
220
221         OPS(sb,disconnect)(ID(sb));
222         list_del(&sbi->ll_list);
223         
224         printk(KERN_INFO "OBDFS: Bye bye.\n");
225
226         MOD_DEC_USE_COUNT;
227         EXIT;
228 } /* ll_put_super */
229
230
231 void ll_do_change_inode(struct inode *inode, int valid)
232 {
233         struct obdo *oa;
234         int err;
235         
236         ENTRY;
237         if (IOPS(inode, setattr) == NULL) {
238                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
239                 EXIT;
240                 return;
241         }
242         oa = obdo_alloc();
243         if ( !oa ) {
244                 printk(__FUNCTION__ ": obdo_alloc failed\n");
245                 EXIT;
246                 return;
247         }
248
249         oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
250         ll_from_inode(oa, inode);
251         oa->o_mode = inode->i_mode;
252         err = IOPS(inode, setattr)(IID(inode), oa);
253
254         if ( err )
255                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
256
257         EXIT;
258         obdo_free(oa);
259 } /* ll_write_inode */
260
261 void ll_change_inode(struct inode *inode, int mask)
262 {
263         return ll_do_change_inode(inode, OBD_MD_FLNLINK); 
264 }
265
266
267 extern void write_inode_pages(struct inode *);
268 /* This routine is called from iput() (for each unlink on the inode).
269  * We can't put this call into delete_inode() since that is called only
270  * when i_count == 0, and we need to keep a reference on the inode while
271  * it is in the page cache, which means i_count > 0.  Catch 22.
272  */
273 static void ll_put_inode(struct inode *inode)
274 {
275         ENTRY;
276         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
277                 write_inode_pages(inode);
278                 EXIT;
279                 return;
280         }
281
282         //ll_dequeue_pages(inode);
283         EXIT;
284 } /* ll_put_inode */
285
286
287 static void ll_delete_inode(struct inode *inode)
288 {
289         ll_do_change_inode(inode, ~0);
290         clear_inode(inode); 
291 }
292 #if 0
293 {
294         struct obdo *oa;
295         int err;
296
297         ENTRY;
298         if (IOPS(inode, destroy) == NULL) {
299                 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
300                 EXIT;
301                 return;
302         }
303
304         oa = obdo_alloc();
305         if ( !oa ) {
306                 printk(__FUNCTION__ ": obdo_alloc failed\n");
307                 EXIT;
308                 return;
309         }
310         oa->o_valid = OBD_MD_FLNOTOBD;
311         ll_from_inode(oa, inode);
312
313         /* XXX how do we know that this inode is now clean? */
314         printk("delete_inode ------> link %d\n", inode->i_nlink);
315         ODEBUG(oa);
316         err = IOPS(inode, destroy)(IID(inode), oa);
317         obdo_free(oa);
318         clear_inode(inode);
319         if (err) {
320                 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
321                 EXIT;
322                 return;
323         }
324
325         EXIT;
326 } /* ll_delete_inode */
327 #endif
328
329
330 static int ll_attr2inode(struct inode * inode, struct iattr * attr)
331 {
332         unsigned int ia_valid = attr->ia_valid;
333         int error = 0;
334
335         if (ia_valid & ATTR_SIZE) {
336                 error = vmtruncate(inode, attr->ia_size);
337                 if (error)
338                         goto out;
339         }
340
341         if (ia_valid & ATTR_UID)
342                 inode->i_uid = attr->ia_uid;
343         if (ia_valid & ATTR_GID)
344                 inode->i_gid = attr->ia_gid;
345         if (ia_valid & ATTR_ATIME)
346                 inode->i_atime = attr->ia_atime;
347         if (ia_valid & ATTR_MTIME)
348                 inode->i_mtime = attr->ia_mtime;
349         if (ia_valid & ATTR_CTIME)
350                 inode->i_ctime = attr->ia_ctime;
351         if (ia_valid & ATTR_MODE) {
352                 inode->i_mode = attr->ia_mode;
353                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
354                         inode->i_mode &= ~S_ISGID;
355         }
356 out:
357         return error;
358 }
359
360 int ll_setattr(struct dentry *de, struct iattr *attr)
361 {
362         struct inode *inode = de->d_inode;
363         struct obdo *oa;
364         int err;
365
366         ENTRY;
367         if (IOPS(inode, setattr) == NULL) {
368                 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
369                 EXIT;
370                 return -EIO;
371         }
372         oa = obdo_alloc();
373         if ( !oa ) {
374                 printk(__FUNCTION__ ": obdo_alloc failed\n");
375                 return -ENOMEM;
376         }
377
378         ll_attr2inode(inode, attr);
379         oa->o_id = inode->i_ino;
380         oa->o_mode = inode->i_mode;
381         obdo_from_iattr(oa, attr);
382         err = IOPS(inode, setattr)(IID(inode), oa);
383
384         if ( err )
385                 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
386
387         EXIT;
388         obdo_free(oa);
389         return err;
390 } /* ll_setattr */
391
392
393
394 static int ll_statfs(struct super_block *sb, struct statfs *buf)
395 {
396         struct statfs tmp;
397         int err;
398
399         ENTRY;
400
401         err = OPS(sb,statfs)(ID(sb), &tmp);
402         if ( err ) { 
403                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
404                 return err;
405         }
406         memcpy(buf, &tmp, sizeof(*buf));
407         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
408         EXIT;
409
410         return err; 
411 }
412
413 static inline void ll_read_inode2(struct inode *inode, void *opaque)
414 {
415         struct mds_rep *rep = opaque; 
416         
417         ENTRY;
418         ll_to_inode(inode, rep); 
419
420         INIT_LIST_HEAD(ll_iplist(inode)); /* list of dirty pages on inode */
421         INIT_LIST_HEAD(ll_islist(inode)); /* list of inodes in superblock */
422
423         /* OIDEBUG(inode); */
424
425         if (S_ISREG(inode->i_mode)) {
426                 inode->i_op = &ll_file_inode_operations;
427                 inode->i_fop = &ll_file_operations;
428                 inode->i_mapping->a_ops = &ll_aops;
429                 EXIT;
430         } else if (S_ISDIR(inode->i_mode)) {
431                 inode->i_op = &ll_dir_inode_operations;
432                 inode->i_fop = &ll_dir_operations; 
433                 inode->i_mapping->a_ops = &ll_aops;
434                 EXIT;
435         } else if (S_ISLNK(inode->i_mode)) {
436                 if (inode->i_blocks) { 
437                         inode->i_op = &ll_symlink_inode_operations;
438                         inode->i_mapping->a_ops = &ll_aops;
439                 }else {
440                         inode->i_op = &ll_fast_symlink_inode_operations;
441                 }
442                 EXIT;
443         } else {
444                 init_special_inode(inode, inode->i_mode,
445                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
446         }
447
448         EXIT;
449         return;
450 }
451
452 /* exported operations */
453 struct super_operations ll_super_operations =
454 {
455         read_inode2: ll_read_inode2,
456         // put_inode: ll_put_inode,
457         // delete_inode: ll_delete_inode,
458         // put_super: ll_put_super,
459         // statfs: ll_statfs
460 };
461
462
463
464 struct file_system_type lustre_light_fs_type = {
465    "lustre_light", 0, ll_read_super, NULL
466 };
467
468 static int __init init_lustre_light(void)
469 {
470         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
471
472         return register_filesystem(&lustre_light_fs_type);
473 }
474
475 static void __exit exit_lustre_light(void)
476 {
477         unregister_filesystem(&lustre_light_fs_type);
478 }
479
480 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
481 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
482 MODULE_LICENSE("GPL");
483
484 module_init(init_lustre_light);
485 module_exit(exit_lustre_light);