Whamcloud - gitweb
No new functionality outside of the DLM. ptlrpc_client no longer contains
[fs/lustre-release.git] / lustre / llite / super.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
10  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11  * Copryright (C) 1999 Seagate Technology Inc.
12  * Copryright (C) 2001 Mountain View Data, Inc.
13  * Copryright (C) 2002 Cluster File Systems, Inc.
14  *
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/stat.h>
23 #include <linux/errno.h>
24 #include <linux/locks.h>
25 #include <linux/unistd.h>
26
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29
30 #include <linux/fs.h>
31 #include <linux/stat.h>
32 #include <asm/uaccess.h>
33 #include <asm/segment.h>
34
35 #define DEBUG_SUBSYSTEM S_LLITE
36
37 #include <linux/lustre_lite.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         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;
99         char *device = NULL;
100         char *version = NULL;
101         int devno;
102         int err;
103         struct ptlrpc_request *request = NULL;
104
105         ENTRY;
106         MOD_INC_USE_COUNT;
107
108         OBD_ALLOC(sbi, sizeof(*sbi));
109         if (!sbi) {
110                 MOD_DEC_USE_COUNT;
111                 RETURN(NULL);
112         }
113
114         sb->u.generic_sbp = sbi;
115
116         ll_options(data, &device, &version);
117
118         if ( !device ) {
119                 CERROR("no device\n");
120                 GOTO(out_free, sb = NULL);
121         }
122
123         devno = simple_strtoul(device, NULL, 0);
124         if ( devno >= MAX_OBD_DEVICES ) {
125                 CERROR("device of %s too high\n", device);
126                 GOTO(out_free, sb = NULL);
127         }
128
129         sbi->ll_conn.oc_dev = &obd_dev[devno];
130         err = obd_connect(&sbi->ll_conn);
131         if ( err ) {
132                 CERROR("cannot connect to %s\n", device);
133                 GOTO(out_free, sb = NULL);
134         }
135
136         /* the first parameter should become an mds device no */
137         ptlrpc_init_client(-1, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
138                            &sbi->ll_mds_client);
139         err = ptlrpc_connect_client("mds", &sbi->ll_mds_client,
140                                     &sbi->ll_mds_peer);
141         if (err) {
142                 CERROR("cannot find MDS\n");
143                 GOTO(out_disc, sb = NULL);
144         }
145
146         sbi->ll_super = sb;
147         sbi->ll_rootino = 2;
148
149         sb->s_maxbytes = 1LL << 36;
150         sb->s_blocksize = PAGE_SIZE;
151         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
152         sb->s_magic = LL_SUPER_MAGIC;
153         sb->s_op = &ll_super_operations;
154
155         /* make root inode */
156         err = mdc_getattr(&sbi->ll_mds_client, &sbi->ll_mds_peer,
157                           sbi->ll_rootino, S_IFDIR,
158                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, &request);
159         if (err) {
160                 CERROR("mdc_getattr failed for root %d\n", err);
161                 GOTO(out_req, sb = NULL);
162         }
163
164         root = iget4(sb, sbi->ll_rootino, NULL,
165                      lustre_msg_buf(request->rq_repmsg, 0));
166         if (root) {
167                 sb->s_root = d_alloc_root(root);
168         } else {
169                 CERROR("lustre_lite: bad iget4 for root\n");
170                 GOTO(out_req, sb = NULL);
171         }
172
173 out_req:
174         ptlrpc_free_req(request);
175         if (!sb) {
176 out_disc:
177                 obd_disconnect(&sbi->ll_conn);
178 out_free:
179                 MOD_DEC_USE_COUNT;
180                 OBD_FREE(sbi, sizeof(*sbi));
181         }
182         if (device) 
183                 OBD_FREE(device, strlen(device) + 1);
184         if (version)
185                 OBD_FREE(version, strlen(version) + 1);
186
187         RETURN(sb);
188 } /* ll_read_super */
189
190 static void ll_put_super(struct super_block *sb)
191 {
192         struct ll_sb_info *sbi = sb->u.generic_sbp;
193         ENTRY;
194         obd_disconnect(&sbi->ll_conn);
195         OBD_FREE(sb->u.generic_sbp, sizeof(*sbi));
196         MOD_DEC_USE_COUNT;
197         EXIT;
198 } /* ll_put_super */
199
200
201 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
202 static void ll_delete_inode(struct inode *inode)
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                         CERROR("no memory\n"); 
210                 }
211
212                 err = obd_destroy(ll_i2obdconn(inode), oa); 
213                 CDEBUG(D_INODE, "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 ptlrpc_request *request;
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_mds_client, &sbi->ll_mds_peer, inode, attr,
266                           &request);
267         if (err)
268                 CERROR("mdc_setattr fails (%d)\n", err);
269
270         ptlrpc_free_req(request);
271
272         EXIT;
273         return err;
274 }
275
276 int ll_setattr(struct dentry *de, struct iattr *attr)
277 {
278         return ll_inode_setattr(de->d_inode, attr, 1);
279 }
280
281 static int ll_statfs(struct super_block *sb, struct statfs *buf)
282 {
283         struct statfs tmp;
284         int err;
285
286         ENTRY;
287
288         err = obd_statfs(ID(sb), &tmp);
289         if ( err ) { 
290                 CERROR("obd_statfs fails (%d)\n", err);
291                 return err;
292         }
293         memcpy(buf, &tmp, sizeof(*buf));
294         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
295         EXIT;
296
297         return err; 
298 }
299
300 static void inline ll_to_inode(struct inode *dst, struct mds_body *body)
301 {
302         struct ll_inode_info *ii = 
303                 (struct ll_inode_info *) &dst->u.generic_ip;
304
305         /* core attributes first */
306         if ( body->valid & OBD_MD_FLID )
307                 dst->i_ino = body->ino;
308         if ( body->valid & OBD_MD_FLATIME ) 
309                 dst->i_atime = body->atime;
310         if ( body->valid & OBD_MD_FLMTIME ) 
311                 dst->i_mtime = body->mtime;
312         if ( body->valid & OBD_MD_FLCTIME ) 
313                 dst->i_ctime = body->ctime;
314         if ( body->valid & OBD_MD_FLSIZE ) 
315                 dst->i_size = body->size;
316         if ( body->valid & OBD_MD_FLMODE ) 
317                 dst->i_mode = body->mode;
318         if ( body->valid & OBD_MD_FLUID ) 
319                 dst->i_uid = body->uid;
320         if ( body->valid & OBD_MD_FLGID ) 
321                 dst->i_gid = body->gid;
322         if ( body->valid & OBD_MD_FLFLAGS ) 
323                 dst->i_flags = body->flags;
324         if ( body->valid & OBD_MD_FLNLINK )
325                 dst->i_nlink = body->nlink;
326         if ( body->valid & OBD_MD_FLGENER )
327                 dst->i_generation = body->generation;
328
329         /* this will become more elaborate for striping etc */ 
330         if (body->valid & OBD_MD_FLOBJID) 
331                 ii->lli_objid = body->objid;
332 #if 0
333
334         if (obdo_has_inline(oa)) {
335                 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
336                     S_ISFIFO(inode->i_mode)) {
337                         obd_rdev rdev = *((obd_rdev *)oa->o_inline);
338                         CDEBUG(D_INODE,
339                                "copying device %x from obdo to inode\n", rdev);
340                         init_special_inode(inode, inode->i_mode, rdev);
341                 } else {
342                         CDEBUG(D_INFO, "copying inline from obdo to inode\n");
343                         memcpy(oinfo->lli_inline, oa->o_inline, OBD_INLINESZ);
344                 }
345                 oinfo->lli_flags |= OBD_FL_INLINEDATA;
346         }
347 #endif 
348 } /* ll_to_inode */
349
350 static inline void ll_read_inode2(struct inode *inode, void *opaque)
351 {
352         struct mds_body *body = opaque; 
353         
354         ENTRY;
355         ll_to_inode(inode, body); 
356
357         /* OIDEBUG(inode); */
358
359         if (S_ISREG(inode->i_mode)) {
360                 inode->i_op = &ll_file_inode_operations;
361                 inode->i_fop = &ll_file_operations;
362                 inode->i_mapping->a_ops = &ll_aops;
363                 EXIT;
364         } else if (S_ISDIR(inode->i_mode)) {
365                 inode->i_op = &ll_dir_inode_operations;
366                 inode->i_fop = &ll_dir_operations; 
367                 inode->i_mapping->a_ops = &ll_dir_aops;
368                 EXIT;
369         } else if (S_ISLNK(inode->i_mode)) {
370                 inode->i_op = &ll_fast_symlink_inode_operations;
371                 EXIT;
372         } else {
373                 init_special_inode(inode, inode->i_mode,
374                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
375         }
376
377         EXIT;
378         return;
379 }
380
381 /* exported operations */
382 struct super_operations ll_super_operations =
383 {
384         read_inode2: ll_read_inode2,
385         delete_inode: ll_delete_inode,
386         put_super: ll_put_super,
387         // statfs: ll_statfs
388 };
389
390 struct file_system_type lustre_lite_fs_type = {
391         "lustre_lite", 0, ll_read_super, NULL
392 };
393
394 static int llite_setup(struct obd_device *dev, obd_count len, void *buf)
395 {
396         MOD_INC_USE_COUNT;
397         return 0;
398 }
399
400 static int llite_cleanup(struct obd_device *dev)
401 {
402         MOD_DEC_USE_COUNT;
403         return 0;
404 }
405
406 /* use obd ops to offer management infrastructure */
407 static struct obd_ops llite_obd_ops = {
408         o_setup:       llite_setup,
409         o_cleanup:     llite_cleanup,
410 };
411
412 static int __init init_lustre_lite(void)
413 {
414         printk(KERN_INFO "Lustre Lite 0.0.1, braam@clusterfs.com\n");
415         obd_register_type(&llite_obd_ops, LUSTRE_LITE_NAME);
416         ll_file_data_slab = kmem_cache_create("ll_file_data",
417                                               sizeof(struct ll_file_data), 0,
418                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
419         if (ll_file_data_slab == NULL)
420                 return -ENOMEM;
421
422         return register_filesystem(&lustre_lite_fs_type);
423 }
424
425 static void __exit exit_lustre_lite(void)
426 {
427         kmem_cache_destroy(ll_file_data_slab);
428         unregister_filesystem(&lustre_lite_fs_type);
429         obd_unregister_type(LUSTRE_LITE_NAME);
430 }
431
432 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
433 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
434 MODULE_LICENSE("GPL");
435
436 module_init(init_lustre_lite);
437 module_exit(exit_lustre_lite);