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