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