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