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