Whamcloud - gitweb
A mostly-fix for "mknod /mnt/lustre/foofo p". It doesn't fail outright
[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) 2002 Cluster File Systems, Inc.
10  */
11
12 #define DEBUG_SUBSYSTEM S_LLITE
13
14 #include <linux/module.h>
15 #include <linux/lustre_lite.h>
16 #include <linux/lustre_ha.h>
17 #include <linux/lustre_dlm.h>
18
19 kmem_cache_t *ll_file_data_slab;
20 extern struct address_space_operations ll_aops;
21 extern struct address_space_operations ll_dir_aops;
22 struct super_operations ll_super_operations;
23
24 extern int ll_recover(struct ptlrpc_client *);
25 extern int ll_commitcbd_setup(struct ll_sb_info *);
26 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
27
28 static char *ll_read_opt(const char *opt, char *data)
29 {
30         char *value;
31         char *retval;
32         ENTRY;
33
34         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
35         if ( strncmp(opt, data, strlen(opt)) )
36                 RETURN(NULL);
37         if ( (value = strchr(data, '=')) == NULL )
38                 RETURN(NULL);
39
40         value++;
41         OBD_ALLOC(retval, strlen(value) + 1);
42         if ( !retval ) {
43                 CERROR("out of memory!\n");
44                 RETURN(NULL);
45         }
46         
47         memcpy(retval, value, strlen(value)+1);
48         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
49         RETURN(retval);
50 }
51
52 static void ll_options(char *options, char **ost, char **mds)
53 {
54         char *this_char;
55         ENTRY;
56
57         if (!options) {
58                 EXIT;
59                 return;
60         }
61
62         for (this_char = strtok (options, ",");
63              this_char != NULL;
64              this_char = strtok (NULL, ",")) {
65                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
66                 if ( (!*ost && (*ost = ll_read_opt("ost", this_char)))||
67                      (!*mds && (*mds = ll_read_opt("mds", this_char))) )
68                         continue;
69         }
70         EXIT;
71 }
72
73 #ifndef log2
74 #define log2(n) ffz(~(n))
75 #endif
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 obd_device *obd;
82         struct ll_sb_info *sbi;
83         char *ost = NULL;
84         char *mds = NULL;
85         int err;
86         struct ll_fid rootfid;
87         struct statfs sfs;
88         __u64 last_committed, last_rcvd;
89         __u32 last_xid;
90         struct ptlrpc_request *request = NULL;
91         struct ll_inode_md md;
92
93         ENTRY;
94         MOD_INC_USE_COUNT;
95
96         OBD_ALLOC(sbi, sizeof(*sbi));
97         if (!sbi) {
98                 MOD_DEC_USE_COUNT;
99                 RETURN(NULL);
100         }
101
102         sb->u.generic_sbp = sbi;
103
104         ll_options(data, &ost, &mds);
105
106         if (!ost) {
107                 CERROR("no ost\n");
108                 GOTO(out_free, sb = NULL);
109         }
110
111         if (!mds) {
112                 CERROR("no mds\n");
113                 GOTO(out_free, sb = NULL);
114         }
115
116         obd = class_uuid2obd(mds); 
117         if (!obd) {
118                 CERROR("MDS %s: not setup or attached\n", mds);
119                 GOTO(out_free, sb = NULL);
120         }
121
122 #if 0
123         err = connmgr_connect(ptlrpc_connmgr, sbi->ll_mds_conn);
124         if (err) {
125                 CERROR("cannot connect to MDS: rc = %d\n", err);
126                 GOTO(out_rpc, sb = NULL);
127         }
128 #endif 
129
130         err = obd_connect(&sbi->ll_mdc_conn, obd);
131         if (err) {
132                 CERROR("cannot connect to %s: rc = %d\n", mds, err);
133                 GOTO(out_free, sb = NULL);
134         }
135         sbi2mdc(sbi)->mdc_conn->c_level = LUSTRE_CONN_FULL;
136
137         obd = class_uuid2obd(ost);
138         if (!obd) {
139                 CERROR("OST %s: not setup or attached\n", ost);
140                 GOTO(out_mdc, sb = NULL);
141         }
142         err = obd_connect(&sbi->ll_osc_conn, obd);
143         if (err) {
144                 CERROR("cannot connect to %s: rc = %d\n", ost, err);
145                 GOTO(out_mdc, sb = NULL);
146         }
147
148         /* XXX: need to store the last_* values somewhere */
149         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid, &last_committed,
150                             &last_rcvd, &last_xid, &request);
151         if (err) {
152                 CERROR("cannot mds_connect: rc = %d\n", err);
153                 GOTO(out_request, sb = NULL);
154         }
155         CDEBUG(D_SUPER, "rootfid %Ld\n", (unsigned long long)rootfid.id);
156         sbi->ll_rootino = rootfid.id;
157
158         memset(&sfs, 0, sizeof(sfs));
159         err = mdc_statfs(&sbi->ll_mdc_conn, &sfs, &request);
160         sb->s_blocksize = sfs.f_bsize;
161         sb->s_blocksize_bits = log2(sfs.f_bsize);
162         sb->s_magic = LL_SUPER_MAGIC;
163         sb->s_maxbytes = (1ULL << (32 + 9)) - sfs.f_bsize;
164         ptlrpc_req_finished(request);
165
166         sb->s_op = &ll_super_operations;
167
168         /* make root inode */
169         err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
170                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
171         if (err) {
172                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
173                 GOTO(out_request, sb = NULL);
174         }
175
176         /* initialize committed transaction callback daemon */
177         spin_lock_init(&sbi->ll_commitcbd_lock);
178         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
179         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
180         sbi->ll_commitcbd_flags = 0;
181         err = ll_commitcbd_setup(sbi);
182         if (err) {
183                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
184                 GOTO(out_request, sb = NULL);
185         }
186
187         md.body = lustre_msg_buf(request->rq_repmsg, 0);
188         md.md = NULL;
189         root = iget4(sb, sbi->ll_rootino, NULL, &md);
190
191         if (root) {
192                 sb->s_root = d_alloc_root(root);
193         } else {
194                 CERROR("lustre_lite: bad iget4 for root\n");
195                 GOTO(out_cdb, sb = NULL);
196         }
197
198         ptlrpc_free_req(request);
199
200 out_dev:
201         if (mds)
202                 OBD_FREE(mds, strlen(mds) + 1);
203         if (ost)
204                 OBD_FREE(ost, strlen(ost) + 1);
205
206         RETURN(sb);
207
208 out_cdb:
209         ll_commitcbd_cleanup(sbi);
210 out_request:
211         ptlrpc_free_req(request);
212         obd_disconnect(&sbi->ll_osc_conn);
213 out_mdc:
214         obd_disconnect(&sbi->ll_mdc_conn);
215 out_free:
216         OBD_FREE(sbi, sizeof(*sbi));
217
218         MOD_DEC_USE_COUNT;
219         goto out_dev;
220 } /* ll_read_super */
221
222 static void ll_put_super(struct super_block *sb)
223 {
224         struct ll_sb_info *sbi = ll_s2sbi(sb);
225         ENTRY;
226         ll_commitcbd_cleanup(sbi);
227         obd_disconnect(&sbi->ll_osc_conn);
228         obd_disconnect(&sbi->ll_mdc_conn);
229         OBD_FREE(sbi, sizeof(*sbi));
230
231         MOD_DEC_USE_COUNT;
232         EXIT;
233 } /* ll_put_super */
234
235 static void ll_clear_inode(struct inode *inode)
236 {
237         if (atomic_read(&inode->i_count) == 0) {
238                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
239                 if (md) {
240                         OBD_FREE(md, md->lmd_size); 
241                         ll_i2info(inode)->lli_smd = NULL;
242                 }
243                 if (ll_i2info(inode)->lli_symlink_name) {
244                         OBD_FREE(ll_i2info(inode)->lli_symlink_name,
245                                  strlen(ll_i2info(inode)->lli_symlink_name)+ 1);
246                         ll_i2info(inode)->lli_symlink_name = NULL;
247                 }
248         }
249 }
250
251 static void ll_delete_inode(struct inode *inode)
252 {
253         if (S_ISREG(inode->i_mode)) { 
254                 int err;
255                 struct obdo oa;
256                 struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
257  
258                if (!md)
259                         GOTO(out, -EINVAL);
260
261                 oa.o_id = md->lmd_object_id;
262                 oa.o_easize = md->lmd_size;
263                 if (oa.o_id == 0) { 
264                         CERROR("This really happens\n"); 
265                         /* No obdo was ever created */
266                         GOTO(out, 0);
267                 }
268
269                 err = obd_destroy(ll_i2obdconn(inode), &oa, md);
270                 CDEBUG(D_SUPER, "obd destroy of %Ld error %d\n",
271                        md->lmd_object_id, err);
272         }
273 out:
274         clear_inode(inode);
275 }
276
277 /* like inode_setattr, but doesn't mark the inode dirty */
278 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
279 {
280         unsigned int ia_valid = attr->ia_valid;
281         int error = 0;
282
283         if ((ia_valid & ATTR_SIZE) && trunc) {
284                 error = vmtruncate(inode, attr->ia_size);
285                 if (error)
286                         goto out;
287         } else if (ia_valid & ATTR_SIZE)
288                 inode->i_size = attr->ia_size;
289
290         if (ia_valid & ATTR_UID)
291                 inode->i_uid = attr->ia_uid;
292         if (ia_valid & ATTR_GID)
293                 inode->i_gid = attr->ia_gid;
294         if (ia_valid & ATTR_ATIME)
295                 inode->i_atime = attr->ia_atime;
296         if (ia_valid & ATTR_MTIME)
297                 inode->i_mtime = attr->ia_mtime;
298         if (ia_valid & ATTR_CTIME)
299                 inode->i_ctime = attr->ia_ctime;
300         if (ia_valid & ATTR_MODE) {
301                 inode->i_mode = attr->ia_mode;
302                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
303                         inode->i_mode &= ~S_ISGID;
304         }
305 out:
306         return error;
307 }
308
309 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
310 {
311         struct ptlrpc_request *request = NULL;
312         struct ll_sb_info *sbi = ll_i2sbi(inode);
313         int err;
314
315         ENTRY;
316
317         /* change incore inode */
318         ll_attr2inode(inode, attr, do_trunc);
319
320         err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
321         if (err)
322                 CERROR("mdc_setattr fails (%d)\n", err);
323
324         ptlrpc_req_finished(request);
325
326         RETURN(err);
327 }
328
329 int ll_setattr(struct dentry *de, struct iattr *attr)
330 {
331         int rc = inode_change_ok(de->d_inode, attr);
332
333         if (rc)
334                 return rc;
335
336         return ll_inode_setattr(de->d_inode, attr, 1);
337 }
338
339 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
340 {
341         struct ptlrpc_request *request = NULL;
342         struct ll_sb_info *sbi = ll_s2sbi(sb);
343         int rc;
344         ENTRY;
345
346         memset(sfs, 0, sizeof(*sfs));
347         rc = mdc_statfs(&sbi->ll_mdc_conn, sfs, &request);
348         ptlrpc_req_finished(request);
349         if (rc)
350                 CERROR("obd_statfs fails: rc = %d\n", rc);
351         else
352                 CDEBUG(D_SUPER, "statfs shows blocks %ld/%ld objects %ld/%ld\n",
353                        sfs->f_bavail, sfs->f_blocks, sfs->f_files,sfs->f_ffree);
354
355         /* temporary until mds_statfs returns statfs info for all OSTs */
356         if (!rc) {
357                 struct statfs obd_sfs;
358
359                 rc = obd_statfs(&sbi->ll_osc_conn, &obd_sfs);
360                 if (rc) {
361                         CERROR("obd_statfs fails: rc = %d\n", rc);
362                         GOTO(out, rc);
363                 }
364                 CDEBUG(D_SUPER, "obd_statfs returns blocks %ld/%ld, "
365                        "objects %ld/%ld\n",
366                        obd_sfs.f_bavail, obd_sfs.f_blocks,
367                        obd_sfs.f_ffree, obd_sfs.f_files);
368
369                 sfs->f_bfree = obd_sfs.f_bfree;
370                 sfs->f_bavail = obd_sfs.f_bavail;
371                 sfs->f_blocks = obd_sfs.f_blocks;
372                 if (obd_sfs.f_ffree < sfs->f_ffree)
373                         sfs->f_ffree = obd_sfs.f_ffree;
374         }
375
376 out:
377         RETURN(rc);
378 }
379
380 inline int ll_stripe_md_size(struct super_block *sb)
381 {
382         struct mdc_obd *mdc = sbi2mdc(ll_s2sbi(sb));
383         return mdc->mdc_max_mdsize;
384 }
385
386 static void ll_to_inode(struct inode *dst, struct ll_inode_md *md)
387 {
388         struct mds_body *body = md->body;
389         struct ll_inode_info *ii = ll_i2info(dst);
390
391         /* core attributes first */
392         if (body->valid & OBD_MD_FLID)
393                 dst->i_ino = body->ino;
394         if (body->valid & OBD_MD_FLATIME)
395                 dst->i_atime = body->atime;
396         if (body->valid & OBD_MD_FLMTIME)
397                 dst->i_mtime = body->mtime;
398         if (body->valid & OBD_MD_FLCTIME)
399                 dst->i_ctime = body->ctime;
400         if (body->valid & OBD_MD_FLSIZE)
401                 dst->i_size = body->size;
402         if (body->valid & OBD_MD_FLMODE)
403                 dst->i_mode = body->mode;
404         if (body->valid & OBD_MD_FLUID)
405                 dst->i_uid = body->uid;
406         if (body->valid & OBD_MD_FLGID)
407                 dst->i_gid = body->gid;
408         if (body->valid & OBD_MD_FLFLAGS)
409                 dst->i_flags = body->flags;
410         if (body->valid & OBD_MD_FLNLINK)
411                 dst->i_nlink = body->nlink;
412         if (body->valid & OBD_MD_FLGENER)
413                 dst->i_generation = body->generation;
414         if (body->valid & OBD_MD_FLRDEV)
415                 dst->i_rdev = body->extra;
416         if (md && md->md && md->md->lmd_stripe_count) { 
417                 struct lov_stripe_md *smd = md->md;
418                 int size = ll_stripe_md_size(dst->i_sb);
419                 if (md->md->lmd_size != size) { 
420                         CERROR("Striping metadata size error %ld\n",
421                                dst->i_ino); 
422                         LBUG();
423                 }
424                 OBD_ALLOC(ii->lli_smd, size);
425                 if (!ii->lli_smd){ 
426                         CERROR("No memory for %d\n", size);
427                         LBUG();
428                 }
429                 memcpy(ii->lli_smd, smd, size);
430         }
431 } /* ll_to_inode */
432
433 static void ll_read_inode2(struct inode *inode, void *opaque)
434 {
435         struct ll_inode_md *md = opaque;
436
437         ENTRY;
438         ll_to_inode(inode, md);
439
440         /* OIDEBUG(inode); */
441
442         if (S_ISREG(inode->i_mode)) {
443                 inode->i_op = &ll_file_inode_operations;
444                 inode->i_fop = &ll_file_operations;
445                 inode->i_mapping->a_ops = &ll_aops;
446                 EXIT;
447         } else if (S_ISDIR(inode->i_mode)) {
448                 inode->i_op = &ll_dir_inode_operations;
449                 inode->i_fop = &ll_dir_operations;
450                 inode->i_mapping->a_ops = &ll_dir_aops;
451                 EXIT;
452         } else if (S_ISLNK(inode->i_mode)) {
453                 inode->i_op = &ll_fast_symlink_inode_operations;
454                 EXIT;
455         } else {
456                 init_special_inode(inode, inode->i_mode,
457                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
458                 EXIT;
459         }
460
461         return;
462 }
463
464 /* exported operations */
465 struct super_operations ll_super_operations =
466 {
467         read_inode2: ll_read_inode2,
468         clear_inode: ll_clear_inode,
469         delete_inode: ll_delete_inode,
470         put_super: ll_put_super,
471         statfs: ll_statfs
472 };
473
474 struct file_system_type lustre_lite_fs_type = {
475         "lustre_lite", 0, ll_read_super, NULL
476 };
477
478 static int __init init_lustre_lite(void)
479 {
480         printk(KERN_INFO "Lustre Lite 0.0.1, info@clusterfs.com\n");
481         ll_file_data_slab = kmem_cache_create("ll_file_data",
482                                               sizeof(struct ll_file_data), 0,
483                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
484         if (ll_file_data_slab == NULL)
485                 return -ENOMEM;
486         return register_filesystem(&lustre_lite_fs_type);
487 }
488
489 static void __exit exit_lustre_lite(void)
490 {
491         unregister_filesystem(&lustre_lite_fs_type);
492         kmem_cache_destroy(ll_file_data_slab);
493 }
494
495 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
496 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
497 MODULE_LICENSE("GPL");
498
499 module_init(init_lustre_lite);
500 module_exit(exit_lustre_lite);