Whamcloud - gitweb
- elimininate the system calls from filter obd
[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 extern struct address_space_operations ll_aops;
39 extern struct address_space_operations ll_dir_aops;
40 struct super_operations ll_super_operations;
41 long obd_memory = 0;
42
43 static char *ll_read_opt(const char *opt, char *data)
44 {
45         char *value;
46         char *retval;
47         ENTRY;
48
49         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
50         if ( strncmp(opt, data, strlen(opt)) ) {
51                 EXIT;
52                 return NULL;
53         }
54         if ( (value = strchr(data, '=')) == NULL ) {
55                 EXIT;
56                 return NULL;
57         }
58
59         value++;
60         OBD_ALLOC(retval, char *, strlen(value) + 1);
61         if ( !retval ) {
62                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
63                 return NULL;
64         }
65         
66         memcpy(retval, value, strlen(value)+1);
67         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
68         EXIT;
69         return retval;
70 }
71
72 static void ll_options(char *options, char **dev, char **vers)
73 {
74         char *this_char;
75         ENTRY; 
76
77         if (!options) { 
78                 EXIT;
79                 return;
80         }
81
82         for (this_char = strtok (options, ",");
83              this_char != NULL;
84              this_char = strtok (NULL, ",")) {
85                 CDEBUG(D_INFO, "this_char %s\n", this_char);
86                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
87                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
88                         continue;
89                 
90         }
91         EXIT;
92 }
93
94 static struct super_block * ll_read_super(struct super_block *sb, 
95                                             void *data, int silent)
96 {
97         struct inode *root = 0; 
98         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
99         char *device = NULL;
100         char *version = NULL;
101         int connected = 0;
102         int devno;
103         int err;
104         struct mds_rep *rep; 
105         struct ptlrep_hdr *hdr = NULL; 
106
107         ENTRY;
108         MOD_INC_USE_COUNT; 
109
110         memset(sbi, 0, sizeof(*sbi));
111
112         ll_options(data, &device, &version);
113         printk(__FUNCTION__ "line %d\n", __LINE__); 
114         if ( !device ) {
115                 printk(__FUNCTION__ ": no device\n");
116                 sb = NULL; 
117                 goto ERR;
118         }
119
120         devno = simple_strtoul(device, NULL, 0);
121         if ( devno >= MAX_OBD_DEVICES ) {
122                 printk(__FUNCTION__ ": device of %s too high\n", device);
123                 sb = NULL; 
124                 goto ERR;
125         } 
126
127         sbi->ll_conn.oc_dev = &obd_dev[devno];
128         err = obd_connect(&sbi->ll_conn);
129         if ( err ) {
130                 printk(__FUNCTION__ "cannot connect to %s\n", device);
131                 sb = NULL; 
132                 goto ERR;
133         }
134         connected = 1;
135
136         err = kportal_uuid_to_peer("mds", &sbi->ll_peer);
137         if (err == 0)
138                 sbi->ll_peer_ptr = &sbi->ll_peer;
139
140         sbi->ll_super = sb;
141         sbi->ll_rootino = 2;
142
143         sb->s_maxbytes = 1LL << 36;
144         sb->s_blocksize = PAGE_SIZE;
145         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
146         sb->s_magic = LL_SUPER_MAGIC;
147         sb->s_op = &ll_super_operations;
148         printk(__FUNCTION__ "line %d\n", __LINE__); 
149
150         /* make root inode */
151         err = mdc_getattr(sbi->ll_peer_ptr, sbi->ll_rootino, S_IFDIR, 
152                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 
153                           &rep, &hdr);
154         if (err) {
155                 printk(__FUNCTION__ ": mds_getattr failed for root %d\n", err);
156                 sb = NULL; 
157                 goto ERR;
158         }
159                          
160         root = iget4(sb, sbi->ll_rootino, NULL, rep);
161         if (root) {
162                 sb->s_root = d_alloc_root(root);
163         } else {
164             printk("lustre_light: bad iget4 for root\n");
165             sb = NULL; 
166             goto ERR;
167         } 
168         
169 ERR:
170         if (hdr)
171                 kfree(hdr);
172         if (device)
173                 OBD_FREE(device, strlen(device) + 1);
174         if (version)
175                 OBD_FREE(version, strlen(version) + 1);
176         if (!sb && connected) 
177                 obd_disconnect(&sbi->ll_conn);
178
179         if (!sb && root) {
180                 iput(root);
181         }
182         if (!sb) 
183                 MOD_DEC_USE_COUNT;
184
185         EXIT;
186         return sb;
187 } /* ll_read_super */
188
189 static void ll_put_super(struct super_block *sb)
190 {
191         ENTRY;
192
193         obd_disconnect(ID(sb));
194
195         MOD_DEC_USE_COUNT;
196         EXIT;
197 } /* ll_put_super */
198
199
200 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
201 static void ll_delete_inode(struct inode *inode)
202 {
203
204         if (S_ISREG(inode->i_mode)) { 
205                 int err; 
206                 struct obdo *oa; 
207                 oa = ll_oa_from_inode(inode, OBD_MD_FLNOTOBD);
208                 if (!oa) { 
209                         printk(__FUNCTION__ ": no memory\n"); 
210                 }
211
212                 err = obd_destroy(IID(inode), oa); 
213                 printk(__FUNCTION__  ": obd destroy of %Ld error %d\n", 
214                        oa->o_id, err);
215                 obdo_free(oa);
216         }
217
218         clear_inode(inode); 
219 }
220
221 /* like inode_setattr, but doesn't mark the inode dirty */ 
222 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
223 {
224         unsigned int ia_valid = attr->ia_valid;
225         int error = 0;
226
227         if ((ia_valid & ATTR_SIZE) && trunc ) {
228                 error = vmtruncate(inode, attr->ia_size);
229                 if (error)
230                         goto out;
231         } else if (ia_valid & ATTR_SIZE) { 
232                 inode->i_size = attr->ia_size;
233         }               
234
235         if (ia_valid & ATTR_UID)
236                 inode->i_uid = attr->ia_uid;
237         if (ia_valid & ATTR_GID)
238                 inode->i_gid = attr->ia_gid;
239         if (ia_valid & ATTR_ATIME)
240                 inode->i_atime = attr->ia_atime;
241         if (ia_valid & ATTR_MTIME)
242                 inode->i_mtime = attr->ia_mtime;
243         if (ia_valid & ATTR_CTIME)
244                 inode->i_ctime = attr->ia_ctime;
245         if (ia_valid & ATTR_MODE) {
246                 inode->i_mode = attr->ia_mode;
247                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
248                         inode->i_mode &= ~S_ISGID;
249         }
250 out:
251         return error;
252 }
253
254 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
255 {
256         struct ptlrep_hdr *hdr = NULL;
257         struct ll_sb_info *sbi = ll_i2sbi(inode);
258         int err;
259
260         ENTRY;
261
262         /* change incore inode */
263         ll_attr2inode(inode, attr, do_trunc);
264
265         err = mdc_setattr(sbi->ll_peer_ptr, inode, attr, NULL, &hdr); 
266         if ( err )
267                 printk(__FUNCTION__ ": ll_setattr fails (%d)\n", err);
268
269         EXIT;
270         return err;
271 }
272
273 int ll_setattr(struct dentry *de, struct iattr *attr)
274 {
275         return ll_inode_setattr(de->d_inode, attr, 1);
276 }
277
278 static int ll_statfs(struct super_block *sb, struct statfs *buf)
279 {
280         struct statfs tmp;
281         int err;
282
283         ENTRY;
284
285         err = obd_statfs(ID(sb), &tmp);
286         if ( err ) { 
287                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
288                 return err;
289         }
290         memcpy(buf, &tmp, sizeof(*buf));
291         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
292         EXIT;
293
294         return err; 
295 }
296
297 static void inline ll_to_inode(struct inode *dst, struct mds_rep *rep)
298 {
299         struct ll_inode_info *ii = 
300                 (struct ll_inode_info *) &dst->u.generic_ip;
301
302         /* core attributes first */
303         if ( rep->valid & OBD_MD_FLID )
304                 dst->i_ino = rep->ino;
305         if ( rep->valid & OBD_MD_FLATIME ) 
306                 dst->i_atime = rep->atime;
307         if ( rep->valid & OBD_MD_FLMTIME ) 
308                 dst->i_mtime = rep->mtime;
309         if ( rep->valid & OBD_MD_FLCTIME ) 
310                 dst->i_ctime = rep->ctime;
311         if ( rep->valid & OBD_MD_FLSIZE ) 
312                 dst->i_size = rep->size;
313         if ( rep->valid & OBD_MD_FLMODE ) 
314                 dst->i_mode = rep->mode;
315         if ( rep->valid & OBD_MD_FLUID ) 
316                 dst->i_uid = rep->uid;
317         if ( rep->valid & OBD_MD_FLGID ) 
318                 dst->i_gid = rep->gid;
319         if ( rep->valid & OBD_MD_FLFLAGS ) 
320                 dst->i_flags = rep->flags;
321         if ( rep->valid & OBD_MD_FLNLINK )
322                 dst->i_nlink = rep->nlink;
323         if ( rep->valid & OBD_MD_FLGENER )
324                 dst->i_generation = rep->generation;
325
326         /* this will become more elaborate for striping etc */ 
327         if (rep->valid & OBD_MD_FLOBJID) 
328                 ii->lli_objid = rep->objid;
329 #if 0
330
331         if (obdo_has_inline(oa)) {
332                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
333                     S_ISFIFO(inode->i_mode)) {
334                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
335                         CDEBUG(D_INODE,
336                                "copying device %x from obdo to inode\n", rdev);
337                         init_special_inode(inode, inode->i_mode, rdev);
338                 } else {
339                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
340                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
341                 }
342                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
343         }
344 #endif 
345 } /* ll_to_inode */
346
347 static inline void ll_read_inode2(struct inode *inode, void *opaque)
348 {
349         struct mds_rep *rep = opaque; 
350         
351         ENTRY;
352         ll_to_inode(inode, rep); 
353
354         /* OIDEBUG(inode); */
355
356         if (S_ISREG(inode->i_mode)) {
357                 inode->i_op = &ll_file_inode_operations;
358                 inode->i_fop = &ll_file_operations;
359                 inode->i_mapping->a_ops = &ll_aops;
360                 EXIT;
361         } else if (S_ISDIR(inode->i_mode)) {
362                 inode->i_op = &ll_dir_inode_operations;
363                 inode->i_fop = &ll_dir_operations; 
364                 inode->i_mapping->a_ops = &ll_dir_aops;
365                 EXIT;
366         } else if (S_ISLNK(inode->i_mode)) {
367                 inode->i_op = &ll_fast_symlink_inode_operations;
368                 EXIT;
369         } else {
370                 init_special_inode(inode, inode->i_mode,
371                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
372         }
373
374         EXIT;
375         return;
376 }
377
378 /* exported operations */
379 struct super_operations ll_super_operations =
380 {
381         read_inode2: ll_read_inode2,
382         delete_inode: ll_delete_inode,
383         put_super: ll_put_super,
384         // statfs: ll_statfs
385 };
386
387 struct file_system_type lustre_light_fs_type = {
388    "lustre_light", 0, ll_read_super, NULL
389 };
390
391 static int __init init_lustre_light(void)
392 {
393         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
394
395         return register_filesystem(&lustre_light_fs_type);
396 }
397
398 static void __exit exit_lustre_light(void)
399 {
400         unregister_filesystem(&lustre_light_fs_type);
401 }
402
403 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
404 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
405 MODULE_LICENSE("GPL");
406
407 module_init(init_lustre_light);
408 module_exit(exit_lustre_light);