Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[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/random.h>
16 #include <linux/version.h>
17 #include <linux/lustre_lite.h>
18 #include <linux/lustre_ha.h>
19 #include <linux/lustre_dlm.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22 #include <linux/lprocfs_status.h>
23
24 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
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
30 extern int ll_recover(struct recovd_data *, int);
31 extern int ll_commitcbd_setup(struct ll_sb_info *);
32 extern int ll_commitcbd_cleanup(struct ll_sb_info *);
33
34 extern void ll_proc_namespace(struct super_block* sb, char* osc, char* mdc);
35
36 static char *ll_read_opt(const char *opt, char *data)
37 {
38         char *value;
39         char *retval;
40         ENTRY;
41
42         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
43         if ( strncmp(opt, data, strlen(opt)) )
44                 RETURN(NULL);
45         if ( (value = strchr(data, '=')) == NULL )
46                 RETURN(NULL);
47
48         value++;
49         OBD_ALLOC(retval, strlen(value) + 1);
50         if ( !retval ) {
51                 CERROR("out of memory!\n");
52                 RETURN(NULL);
53         }
54
55         memcpy(retval, value, strlen(value)+1);
56         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
57         RETURN(retval);
58 }
59
60 static int ll_set_opt(const char *opt, char *data, int fl)
61 {
62         ENTRY;
63
64         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
65         if ( strncmp(opt, data, strlen(opt)) )
66                 RETURN(0);
67         else
68                 RETURN(fl);
69 }
70
71 static void ll_options(char *options, char **ost, char **mds, int *flags)
72 {
73         char *this_char;
74         ENTRY;
75
76         if (!options) {
77                 EXIT;
78                 return;
79         }
80
81         for (this_char = strtok (options, ",");
82              this_char != NULL;
83              this_char = strtok (NULL, ",")) {
84                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
85                 if ( (!*ost && (*ost = ll_read_opt("osc", this_char)))||
86                      (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
87                      (!(*flags & LL_SBI_NOLCK) && ((*flags) = (*flags) |
88                       ll_set_opt("nolock", this_char, LL_SBI_NOLCK))) )
89                         continue;
90         }
91         EXIT;
92 }
93
94 #ifndef log2
95 #define log2(n) ffz(~(n))
96 #endif
97
98 static struct super_block *ll_read_super(struct super_block *sb,
99                                          void *data, int silent)
100 {
101         struct inode *root = 0;
102         struct obd_device *obd;
103         struct ll_sb_info *sbi;
104         char *osc = NULL;
105         char *mdc = NULL;
106         int err;
107         struct ll_fid rootfid;
108         struct obd_statfs osfs;
109         struct ptlrpc_request *request = NULL;
110         struct ptlrpc_connection *mdc_conn;
111         struct ll_read_inode2_cookie lic;
112         class_uuid_t uuid;
113
114         ENTRY;
115
116         OBD_ALLOC(sbi, sizeof(*sbi));
117         if (!sbi)
118                 RETURN(NULL);
119
120         INIT_LIST_HEAD(&sbi->ll_conn_chain);
121         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
122         generate_random_uuid(uuid);
123         class_uuid_unparse(uuid, sbi->ll_sb_uuid);
124
125         sb->u.generic_sbp = sbi;
126
127         ll_options(data, &osc, &mdc, &sbi->ll_flags);
128
129         if (!osc) {
130                 CERROR("no osc\n");
131                 GOTO(out_free, sb = NULL);
132         }
133
134         if (!mdc) {
135                 CERROR("no mdc\n");
136                 GOTO(out_free, sb = NULL);
137         }
138
139         obd = class_uuid2obd(mdc);
140         if (!obd) {
141                 CERROR("MDC %s: not setup or attached\n", mdc);
142                 GOTO(out_free, sb = NULL);
143         }
144
145         err = obd_connect(&sbi->ll_mdc_conn, obd, sbi->ll_sb_uuid,
146                           ptlrpc_recovd, ll_recover);
147         if (err) {
148                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
149                 GOTO(out_free, sb = NULL);
150         }
151
152         mdc_conn = sbi2mdc(sbi)->cl_import.imp_connection;
153         list_add(&mdc_conn->c_sb_chain, &sbi->ll_conn_chain);
154
155         obd = class_uuid2obd(osc);
156         if (!obd) {
157                 CERROR("OSC %s: not setup or attached\n", osc);
158                 GOTO(out_mdc, sb = NULL);
159         }
160
161         err = obd_connect(&sbi->ll_osc_conn, obd, sbi->ll_sb_uuid,
162                           ptlrpc_recovd, ll_recover);
163         if (err) {
164                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
165                 GOTO(out_mdc, sb = NULL);
166         }
167
168         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
169         if (err) {
170                 CERROR("cannot mds_connect: rc = %d\n", err);
171                 GOTO(out_mdc, sb = NULL);
172         }
173         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
174         sbi->ll_rootino = rootfid.id;
175
176         memset(&osfs, 0, sizeof(osfs));
177         err = obd_statfs(&sbi->ll_mdc_conn, &osfs);
178         sb->s_blocksize = osfs.os_bsize;
179         sb->s_blocksize_bits = log2(osfs.os_bsize);
180         sb->s_magic = LL_SUPER_MAGIC;
181         sb->s_maxbytes = (1ULL << (32 + 9)) - osfs.os_bsize;
182
183         sb->s_op = &ll_super_operations;
184
185         /* make root inode */
186         err = mdc_getattr(&sbi->ll_mdc_conn, sbi->ll_rootino, S_IFDIR,
187                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
188         if (err) {
189                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
190                 GOTO(out_request, sb = NULL);
191         }
192
193         /* initialize committed transaction callback daemon */
194         spin_lock_init(&sbi->ll_commitcbd_lock);
195         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
196         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
197         sbi->ll_commitcbd_flags = 0;
198         err = ll_commitcbd_setup(sbi);
199         if (err) {
200                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
201                 GOTO(out_request, sb = NULL);
202         }
203
204         lic.lic_body = lustre_msg_buf(request->rq_repmsg, 0);
205         lic.lic_lmm = NULL;
206         LASSERT(sbi->ll_rootino != 0);
207         root = iget4(sb, sbi->ll_rootino, NULL, &lic);
208
209         if (root) {
210                 sb->s_root = d_alloc_root(root);
211         } else {
212                 CERROR("lustre_lite: bad iget4 for root\n");
213                 GOTO(out_cdb, sb = NULL);
214         }
215
216         ptlrpc_req_finished(request);
217         request = NULL;
218         ll_proc_namespace(sb, osc, mdc);
219
220 out_dev:
221         if (mdc)
222                 OBD_FREE(mdc, strlen(mdc) + 1);
223         if (osc)
224                 OBD_FREE(osc, strlen(osc) + 1);
225
226         RETURN(sb);
227
228 out_cdb:
229         ll_commitcbd_cleanup(sbi);
230 out_request:
231         ptlrpc_req_finished(request);
232         obd_disconnect(&sbi->ll_osc_conn);
233 out_mdc:
234         obd_disconnect(&sbi->ll_mdc_conn);
235 out_free:
236         OBD_FREE(sbi, sizeof(*sbi));
237
238         goto out_dev;
239 } /* ll_read_super */
240
241 static void ll_put_super(struct super_block *sb)
242 {
243         struct ll_sb_info *sbi = ll_s2sbi(sb);
244         struct list_head *tmp, *next;
245         struct ll_fid rootfid;
246         ENTRY;
247
248         list_del(&sbi->ll_conn_chain);
249         ll_commitcbd_cleanup(sbi);
250         obd_disconnect(&sbi->ll_osc_conn);
251
252         /* NULL request to force sync on the MDS, and get the last_committed
253          * value to flush remaining RPCs from the sending queue on client.
254          *
255          * XXX This should be an mdc_sync() call to sync the whole MDS fs,
256          *     which we can call for other reasons as well.
257          */
258         mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
259
260         lprocfs_dereg_mnt(sbi->ll_proc_root);
261         sbi->ll_proc_root = NULL;
262
263         obd_disconnect(&sbi->ll_mdc_conn);
264
265         spin_lock(&dcache_lock);
266         list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
267                 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
268                 shrink_dcache_parent(dentry);
269         }
270         spin_unlock(&dcache_lock);
271
272         OBD_FREE(sbi, sizeof(*sbi));
273
274         EXIT;
275 } /* ll_put_super */
276
277 static void ll_clear_inode(struct inode *inode)
278 {
279         struct ll_sb_info *sbi = ll_i2sbi(inode);
280         struct ll_inode_info *lli = ll_i2info(inode);
281         int rc;
282         ENTRY;
283
284         rc = mdc_cancel_unused(&sbi->ll_mdc_conn, inode, LDLM_FL_NO_CALLBACK);
285         if (rc < 0) {
286                 CERROR("mdc_cancel_unused: %d\n", rc);
287                 /* XXX FIXME do something dramatic */
288         }
289
290         if (lli->lli_smd) {
291                 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd, 0);
292                 if (rc < 0) {
293                         CERROR("obd_cancel_unused: %d\n", rc);
294                         /* XXX FIXME do something dramatic */
295                 }
296         }
297
298         if (atomic_read(&inode->i_count) != 0)
299                 CERROR("clearing in-use inode %lu: count = %d\n",
300                        inode->i_ino, atomic_read(&inode->i_count));
301
302         if (lli->lli_smd)
303                 obd_free_memmd(&sbi->ll_osc_conn, &lli->lli_smd);
304
305         if (lli->lli_symlink_name) {
306                 OBD_FREE(lli->lli_symlink_name,strlen(lli->lli_symlink_name)+1);
307                 lli->lli_symlink_name = NULL;
308         }
309
310         EXIT;
311 }
312
313 static void ll_delete_inode(struct inode *inode)
314 {
315         ENTRY;
316         if (S_ISREG(inode->i_mode)) {
317                 int err;
318                 struct obdo *oa;
319                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
320
321                 /* mcreate with no open */
322                 if (!lsm)
323                         GOTO(out, 0);
324
325                 if (lsm->lsm_object_id == 0) {
326                         CERROR("This really happens\n");
327                         /* No obdo was ever created */
328                         GOTO(out, 0);
329                 }
330
331                 oa = obdo_alloc();
332                 if (oa == NULL)
333                         GOTO(out, -ENOMEM);
334
335                 oa->o_id = lsm->lsm_object_id;
336                 obdo_from_inode(oa, inode, OBD_MD_FLID | OBD_MD_FLTYPE);
337
338                 err = obd_destroy(ll_i2obdconn(inode), oa, lsm);
339                 obdo_free(oa);
340                 if (err)
341                         CDEBUG(D_SUPER, "obd destroy objid "LPX64" error %d\n",
342                                lsm->lsm_object_id, err);
343         }
344 out:
345         clear_inode(inode);
346         EXIT;
347 }
348
349 /* like inode_setattr, but doesn't mark the inode dirty */
350 static int ll_attr2inode(struct inode * inode, struct iattr * attr, int trunc)
351 {
352         unsigned int ia_valid = attr->ia_valid;
353         int error = 0;
354
355         if ((ia_valid & ATTR_SIZE) && trunc) {
356                 error = vmtruncate(inode, attr->ia_size);
357                 if (error)
358                         goto out;
359         } else if (ia_valid & ATTR_SIZE)
360                 inode->i_size = attr->ia_size;
361
362         if (ia_valid & ATTR_UID)
363                 inode->i_uid = attr->ia_uid;
364         if (ia_valid & ATTR_GID)
365                 inode->i_gid = attr->ia_gid;
366         if (ia_valid & ATTR_ATIME)
367                 inode->i_atime = attr->ia_atime;
368         if (ia_valid & ATTR_MTIME)
369                 inode->i_mtime = attr->ia_mtime;
370         if (ia_valid & ATTR_CTIME)
371                 inode->i_ctime = attr->ia_ctime;
372         if (ia_valid & ATTR_MODE) {
373                 inode->i_mode = attr->ia_mode;
374                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
375                         inode->i_mode &= ~S_ISGID;
376         }
377 out:
378         return error;
379 }
380
381 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
382 {
383         struct ptlrpc_request *request = NULL;
384         struct ll_sb_info *sbi = ll_i2sbi(inode);
385         int err = 0;
386         ENTRY;
387
388         /* change incore inode */
389         ll_attr2inode(inode, attr, do_trunc);
390
391         /* Don't send size changes to MDS to avoid "fast EA" problems, and
392          * also avoid a pointless RPC (we get file size from OST anyways).
393          */
394         attr->ia_valid &= ~ATTR_SIZE;
395         if (attr->ia_valid) {
396                 err = mdc_setattr(&sbi->ll_mdc_conn, inode, attr, &request);
397                 if (err)
398                         CERROR("mdc_setattr fails (%d)\n", err);
399
400                 ptlrpc_req_finished(request);
401         }
402
403         RETURN(err);
404 }
405
406 int ll_setattr(struct dentry *de, struct iattr *attr)
407 {
408         int rc = inode_change_ok(de->d_inode, attr);
409
410         if (rc)
411                 return rc;
412
413         return ll_inode_setattr(de->d_inode, attr, 1);
414 }
415
416 static int ll_statfs(struct super_block *sb, struct statfs *sfs)
417 {
418         struct ll_sb_info *sbi = ll_s2sbi(sb);
419         struct obd_statfs osfs;
420         int rc;
421         ENTRY;
422
423         memset(sfs, 0, sizeof(*sfs));
424         rc = obd_statfs(&sbi->ll_mdc_conn, &osfs);
425         statfs_unpack(sfs, &osfs);
426         if (rc)
427                 CERROR("mdc_statfs fails: rc = %d\n", rc);
428         else
429                 CDEBUG(D_SUPER, "mdc_statfs shows blocks "LPU64"/"LPU64
430                        " objects "LPU64"/"LPU64"\n",
431                        osfs.os_bavail, osfs.os_blocks,
432                        osfs.os_ffree, osfs.os_files);
433
434         /* temporary until mds_statfs returns statfs info for all OSTs */
435         if (!rc) {
436                 rc = obd_statfs(&sbi->ll_osc_conn, &osfs);
437                 if (rc) {
438                         CERROR("obd_statfs fails: rc = %d\n", rc);
439                         GOTO(out, rc);
440                 }
441                 CDEBUG(D_SUPER, "obd_statfs shows blocks "LPU64"/"LPU64
442                        " objects "LPU64"/"LPU64"\n",
443                        osfs.os_bavail, osfs.os_blocks,
444                        osfs.os_ffree, osfs.os_files);
445
446                 while (osfs.os_blocks > ~0UL) {
447                         sfs->f_bsize <<= 1;
448
449                         osfs.os_blocks >>= 1;
450                         osfs.os_bfree >>= 1;
451                         osfs.os_bavail >>= 1;
452                 }
453                 sfs->f_blocks = osfs.os_blocks;
454                 sfs->f_bfree = osfs.os_bfree;
455                 sfs->f_bavail = osfs.os_bavail;
456                 if (osfs.os_ffree < (__u64)sfs->f_ffree)
457                         sfs->f_ffree = osfs.os_ffree;
458         }
459
460 out:
461         RETURN(rc);
462 }
463
464 void ll_update_inode(struct inode *inode, struct mds_body *body)
465 {
466         if (body->valid & OBD_MD_FLID)
467                 inode->i_ino = body->ino;
468         if (body->valid & OBD_MD_FLATIME)
469                 inode->i_atime = body->atime;
470         if (body->valid & OBD_MD_FLMTIME)
471                 inode->i_mtime = body->mtime;
472         if (body->valid & OBD_MD_FLCTIME)
473                 inode->i_ctime = body->ctime;
474         if (body->valid & OBD_MD_FLMODE)
475                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
476         if (body->valid & OBD_MD_FLTYPE)
477                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
478         if (body->valid & OBD_MD_FLUID)
479                 inode->i_uid = body->uid;
480         if (body->valid & OBD_MD_FLGID)
481                 inode->i_gid = body->gid;
482         if (body->valid & OBD_MD_FLFLAGS)
483                 inode->i_flags = body->flags;
484         if (body->valid & OBD_MD_FLNLINK)
485                 inode->i_nlink = body->nlink;
486         if (body->valid & OBD_MD_FLGENER)
487                 inode->i_generation = body->generation;
488         if (body->valid & OBD_MD_FLRDEV)
489                 inode->i_rdev = body->rdev;
490         if (body->valid & OBD_MD_FLSIZE)
491                 inode->i_size = body->size;
492 }
493
494 static void ll_read_inode2(struct inode *inode, void *opaque)
495 {
496         struct ll_read_inode2_cookie *lic = opaque;
497         struct mds_body *body = lic->lic_body;
498         struct ll_inode_info *lli = ll_i2info(inode);
499         ENTRY;
500
501         sema_init(&lli->lli_open_sem, 1);
502         atomic_set(&lli->lli_open_count, 0);
503
504         /* core attributes first */
505         ll_update_inode(inode, body);
506
507         LASSERT(!lli->lli_smd);
508         if (lic && lic->lic_lmm)
509                 obd_unpackmd(ll_i2obdconn(inode), &lli->lli_smd, lic->lic_lmm);
510
511         /* Get the authoritative file size */
512         if (lli->lli_smd && (inode->i_mode & S_IFREG)) {
513                 int rc;
514                 LASSERT(lli->lli_smd->lsm_object_id != 0);
515                 rc = ll_file_size(inode, lli->lli_smd);
516                 if (rc) {
517                         CERROR("ll_file_size: %d\n", rc);
518                         ll_clear_inode(inode);
519                         make_bad_inode(inode);
520                 }
521         }
522
523         /* OIDEBUG(inode); */
524
525         if (S_ISREG(inode->i_mode)) {
526                 inode->i_op = &ll_file_inode_operations;
527                 inode->i_fop = &ll_file_operations;
528                 inode->i_mapping->a_ops = &ll_aops;
529                 EXIT;
530         } else if (S_ISDIR(inode->i_mode)) {
531                 inode->i_op = &ll_dir_inode_operations;
532                 inode->i_fop = &ll_dir_operations;
533                 inode->i_mapping->a_ops = &ll_dir_aops;
534                 EXIT;
535         } else if (S_ISLNK(inode->i_mode)) {
536                 inode->i_op = &ll_fast_symlink_inode_operations;
537                 EXIT;
538         } else {
539                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
540                 EXIT;
541         }
542 }
543
544 static inline void invalidate_request_list(struct list_head *req_list)
545 {
546         struct list_head *tmp, *n;
547         list_for_each_safe(tmp, n, req_list) {
548                 struct ptlrpc_request *req =
549                         list_entry(tmp, struct ptlrpc_request, rq_list);
550                 CERROR("invalidating req xid "LPU64" op %d to %s:%d\n",
551                        req->rq_xid, req->rq_reqmsg->opc,
552                        req->rq_connection->c_remote_uuid,
553                        req->rq_import->imp_client->cli_request_portal);
554                 req->rq_flags |= PTL_RPC_FL_ERR;
555                 wake_up(&req->rq_wait_for_rep);
556         }
557 }
558
559 void ll_umount_begin(struct super_block *sb)
560 {
561         struct ll_sb_info *sbi = ll_s2sbi(sb);
562         struct list_head *ctmp;
563
564         ENTRY;
565
566         list_for_each(ctmp, &sbi->ll_conn_chain) {
567                 struct ptlrpc_connection *conn;
568                 conn = list_entry(ctmp, struct ptlrpc_connection, c_sb_chain);
569
570                 spin_lock(&conn->c_lock);
571                 /* XXX should just be dealing with imports, probably through
572                  * XXX iocontrol, need next-gen recovery! */
573                 conn->c_flags |= CONN_INVALID;
574                 /* invalidate_request_list(&conn->c_sending_head); */
575                 invalidate_request_list(&conn->c_delayed_head);
576                 spin_unlock(&conn->c_lock);
577         }
578
579         EXIT;
580 }
581
582 /* exported operations */
583 struct super_operations ll_super_operations =
584 {
585         read_inode2: ll_read_inode2,
586         clear_inode: ll_clear_inode,
587         delete_inode: ll_delete_inode,
588         put_super: ll_put_super,
589         statfs: ll_statfs,
590         umount_begin: ll_umount_begin
591 };
592
593 static struct file_system_type lustre_lite_fs_type = {
594         name:           "lustre_lite",
595         fs_flags:       0,
596         read_super:     ll_read_super,
597         owner:          THIS_MODULE,
598 };
599
600 static int __init init_lustre_lite(void)
601 {
602         printk(KERN_INFO "Lustre Lite 0.5.14, info@clusterfs.com\n");
603         ll_file_data_slab = kmem_cache_create("ll_file_data",
604                                               sizeof(struct ll_file_data), 0,
605                                               SLAB_HWCACHE_ALIGN, NULL, NULL);
606         if (ll_file_data_slab == NULL)
607                 return -ENOMEM;
608         return register_filesystem(&lustre_lite_fs_type);
609 }
610
611 static void __exit exit_lustre_lite(void)
612 {
613         unregister_filesystem(&lustre_lite_fs_type);
614         kmem_cache_destroy(ll_file_data_slab);
615 }
616
617 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
618 MODULE_DESCRIPTION("Lustre Lite Client File System v1.0");
619 MODULE_LICENSE("GPL");
620
621 module_init(init_lustre_lite);
622 module_exit(exit_lustre_lite);
623 #endif