]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/ocfs2/dlm/dlmfs.c
Merge branch 'linux-2.6'
[mirror_ubuntu-hirsute-kernel.git] / fs / ocfs2 / dlm / dlmfs.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmfs.c
5 *
6 * Code which implements the kernel side of a minimal userspace
7 * interface to our DLM. This file handles the virtual file system
8 * used for communication with userspace. Credit should go to ramfs,
9 * which was a template for the fs side of this module.
10 *
11 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public
24 * License along with this program; if not, write to the
25 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 * Boston, MA 021110-1307, USA.
27 */
28
29 /* Simple VFS hooks based on: */
30 /*
31 * Resizable simple ram filesystem for Linux.
32 *
33 * Copyright (C) 2000 Linus Torvalds.
34 * 2000 Transmeta Corp.
35 */
36
37 #include <linux/module.h>
38 #include <linux/fs.h>
39 #include <linux/pagemap.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43 #include <linux/init.h>
44 #include <linux/string.h>
45 #include <linux/smp_lock.h>
46 #include <linux/backing-dev.h>
47
48 #include <asm/uaccess.h>
49
50
51 #include "cluster/nodemanager.h"
52 #include "cluster/heartbeat.h"
53 #include "cluster/tcp.h"
54
55 #include "dlmapi.h"
56
57 #include "userdlm.h"
58
59 #include "dlmfsver.h"
60
61 #define MLOG_MASK_PREFIX ML_DLMFS
62 #include "cluster/masklog.h"
63
64 static const struct super_operations dlmfs_ops;
65 static const struct file_operations dlmfs_file_operations;
66 static const struct inode_operations dlmfs_dir_inode_operations;
67 static const struct inode_operations dlmfs_root_inode_operations;
68 static const struct inode_operations dlmfs_file_inode_operations;
69 static struct kmem_cache *dlmfs_inode_cache;
70
71 struct workqueue_struct *user_dlm_worker;
72
73 /*
74 * decodes a set of open flags into a valid lock level and a set of flags.
75 * returns < 0 if we have invalid flags
76 * flags which mean something to us:
77 * O_RDONLY -> PRMODE level
78 * O_WRONLY -> EXMODE level
79 *
80 * O_NONBLOCK -> LKM_NOQUEUE
81 */
82 static int dlmfs_decode_open_flags(int open_flags,
83 int *level,
84 int *flags)
85 {
86 if (open_flags & (O_WRONLY|O_RDWR))
87 *level = LKM_EXMODE;
88 else
89 *level = LKM_PRMODE;
90
91 *flags = 0;
92 if (open_flags & O_NONBLOCK)
93 *flags |= LKM_NOQUEUE;
94
95 return 0;
96 }
97
98 static int dlmfs_file_open(struct inode *inode,
99 struct file *file)
100 {
101 int status, level, flags;
102 struct dlmfs_filp_private *fp = NULL;
103 struct dlmfs_inode_private *ip;
104
105 if (S_ISDIR(inode->i_mode))
106 BUG();
107
108 mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
109 file->f_flags);
110
111 status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
112 if (status < 0)
113 goto bail;
114
115 /* We don't want to honor O_APPEND at read/write time as it
116 * doesn't make sense for LVB writes. */
117 file->f_flags &= ~O_APPEND;
118
119 fp = kmalloc(sizeof(*fp), GFP_NOFS);
120 if (!fp) {
121 status = -ENOMEM;
122 goto bail;
123 }
124 fp->fp_lock_level = level;
125
126 ip = DLMFS_I(inode);
127
128 status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
129 if (status < 0) {
130 /* this is a strange error to return here but I want
131 * to be able userspace to be able to distinguish a
132 * valid lock request from one that simply couldn't be
133 * granted. */
134 if (flags & LKM_NOQUEUE && status == -EAGAIN)
135 status = -ETXTBSY;
136 kfree(fp);
137 goto bail;
138 }
139
140 file->private_data = fp;
141 bail:
142 return status;
143 }
144
145 static int dlmfs_file_release(struct inode *inode,
146 struct file *file)
147 {
148 int level, status;
149 struct dlmfs_inode_private *ip = DLMFS_I(inode);
150 struct dlmfs_filp_private *fp =
151 (struct dlmfs_filp_private *) file->private_data;
152
153 if (S_ISDIR(inode->i_mode))
154 BUG();
155
156 mlog(0, "close called on inode %lu\n", inode->i_ino);
157
158 status = 0;
159 if (fp) {
160 level = fp->fp_lock_level;
161 if (level != LKM_IVMODE)
162 user_dlm_cluster_unlock(&ip->ip_lockres, level);
163
164 kfree(fp);
165 file->private_data = NULL;
166 }
167
168 return 0;
169 }
170
171 static ssize_t dlmfs_file_read(struct file *filp,
172 char __user *buf,
173 size_t count,
174 loff_t *ppos)
175 {
176 int bytes_left;
177 ssize_t readlen;
178 char *lvb_buf;
179 struct inode *inode = filp->f_path.dentry->d_inode;
180
181 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
182 inode->i_ino, count, *ppos);
183
184 if (*ppos >= i_size_read(inode))
185 return 0;
186
187 if (!count)
188 return 0;
189
190 if (!access_ok(VERIFY_WRITE, buf, count))
191 return -EFAULT;
192
193 /* don't read past the lvb */
194 if ((count + *ppos) > i_size_read(inode))
195 readlen = i_size_read(inode) - *ppos;
196 else
197 readlen = count - *ppos;
198
199 lvb_buf = kmalloc(readlen, GFP_NOFS);
200 if (!lvb_buf)
201 return -ENOMEM;
202
203 user_dlm_read_lvb(inode, lvb_buf, readlen);
204 bytes_left = __copy_to_user(buf, lvb_buf, readlen);
205 readlen -= bytes_left;
206
207 kfree(lvb_buf);
208
209 *ppos = *ppos + readlen;
210
211 mlog(0, "read %zd bytes\n", readlen);
212 return readlen;
213 }
214
215 static ssize_t dlmfs_file_write(struct file *filp,
216 const char __user *buf,
217 size_t count,
218 loff_t *ppos)
219 {
220 int bytes_left;
221 ssize_t writelen;
222 char *lvb_buf;
223 struct inode *inode = filp->f_path.dentry->d_inode;
224
225 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
226 inode->i_ino, count, *ppos);
227
228 if (*ppos >= i_size_read(inode))
229 return -ENOSPC;
230
231 if (!count)
232 return 0;
233
234 if (!access_ok(VERIFY_READ, buf, count))
235 return -EFAULT;
236
237 /* don't write past the lvb */
238 if ((count + *ppos) > i_size_read(inode))
239 writelen = i_size_read(inode) - *ppos;
240 else
241 writelen = count - *ppos;
242
243 lvb_buf = kmalloc(writelen, GFP_NOFS);
244 if (!lvb_buf)
245 return -ENOMEM;
246
247 bytes_left = copy_from_user(lvb_buf, buf, writelen);
248 writelen -= bytes_left;
249 if (writelen)
250 user_dlm_write_lvb(inode, lvb_buf, writelen);
251
252 kfree(lvb_buf);
253
254 *ppos = *ppos + writelen;
255 mlog(0, "wrote %zd bytes\n", writelen);
256 return writelen;
257 }
258
259 static void dlmfs_init_once(void *foo,
260 struct kmem_cache *cachep,
261 unsigned long flags)
262 {
263 struct dlmfs_inode_private *ip =
264 (struct dlmfs_inode_private *) foo;
265
266 if (flags & SLAB_CTOR_CONSTRUCTOR) {
267 ip->ip_dlm = NULL;
268 ip->ip_parent = NULL;
269
270 inode_init_once(&ip->ip_vfs_inode);
271 }
272 }
273
274 static struct inode *dlmfs_alloc_inode(struct super_block *sb)
275 {
276 struct dlmfs_inode_private *ip;
277
278 ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
279 if (!ip)
280 return NULL;
281
282 return &ip->ip_vfs_inode;
283 }
284
285 static void dlmfs_destroy_inode(struct inode *inode)
286 {
287 kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
288 }
289
290 static void dlmfs_clear_inode(struct inode *inode)
291 {
292 int status;
293 struct dlmfs_inode_private *ip;
294
295 if (!inode)
296 return;
297
298 mlog(0, "inode %lu\n", inode->i_ino);
299
300 ip = DLMFS_I(inode);
301
302 if (S_ISREG(inode->i_mode)) {
303 status = user_dlm_destroy_lock(&ip->ip_lockres);
304 if (status < 0)
305 mlog_errno(status);
306 iput(ip->ip_parent);
307 goto clear_fields;
308 }
309
310 mlog(0, "we're a directory, ip->ip_dlm = 0x%p\n", ip->ip_dlm);
311 /* we must be a directory. If required, lets unregister the
312 * dlm context now. */
313 if (ip->ip_dlm)
314 user_dlm_unregister_context(ip->ip_dlm);
315 clear_fields:
316 ip->ip_parent = NULL;
317 ip->ip_dlm = NULL;
318 }
319
320 static struct backing_dev_info dlmfs_backing_dev_info = {
321 .ra_pages = 0, /* No readahead */
322 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
323 };
324
325 static struct inode *dlmfs_get_root_inode(struct super_block *sb)
326 {
327 struct inode *inode = new_inode(sb);
328 int mode = S_IFDIR | 0755;
329 struct dlmfs_inode_private *ip;
330
331 if (inode) {
332 ip = DLMFS_I(inode);
333
334 inode->i_mode = mode;
335 inode->i_uid = current->fsuid;
336 inode->i_gid = current->fsgid;
337 inode->i_blocks = 0;
338 inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
339 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
340 inc_nlink(inode);
341
342 inode->i_fop = &simple_dir_operations;
343 inode->i_op = &dlmfs_root_inode_operations;
344 }
345
346 return inode;
347 }
348
349 static struct inode *dlmfs_get_inode(struct inode *parent,
350 struct dentry *dentry,
351 int mode)
352 {
353 struct super_block *sb = parent->i_sb;
354 struct inode * inode = new_inode(sb);
355 struct dlmfs_inode_private *ip;
356
357 if (!inode)
358 return NULL;
359
360 inode->i_mode = mode;
361 inode->i_uid = current->fsuid;
362 inode->i_gid = current->fsgid;
363 inode->i_blocks = 0;
364 inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
365 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
366
367 ip = DLMFS_I(inode);
368 ip->ip_dlm = DLMFS_I(parent)->ip_dlm;
369
370 switch (mode & S_IFMT) {
371 default:
372 /* for now we don't support anything other than
373 * directories and regular files. */
374 BUG();
375 break;
376 case S_IFREG:
377 inode->i_op = &dlmfs_file_inode_operations;
378 inode->i_fop = &dlmfs_file_operations;
379
380 i_size_write(inode, DLM_LVB_LEN);
381
382 user_dlm_lock_res_init(&ip->ip_lockres, dentry);
383
384 /* released at clear_inode time, this insures that we
385 * get to drop the dlm reference on each lock *before*
386 * we call the unregister code for releasing parent
387 * directories. */
388 ip->ip_parent = igrab(parent);
389 BUG_ON(!ip->ip_parent);
390 break;
391 case S_IFDIR:
392 inode->i_op = &dlmfs_dir_inode_operations;
393 inode->i_fop = &simple_dir_operations;
394
395 /* directory inodes start off with i_nlink ==
396 * 2 (for "." entry) */
397 inc_nlink(inode);
398 break;
399 }
400
401 if (parent->i_mode & S_ISGID) {
402 inode->i_gid = parent->i_gid;
403 if (S_ISDIR(mode))
404 inode->i_mode |= S_ISGID;
405 }
406
407 return inode;
408 }
409
410 /*
411 * File creation. Allocate an inode, and we're done..
412 */
413 /* SMP-safe */
414 static int dlmfs_mkdir(struct inode * dir,
415 struct dentry * dentry,
416 int mode)
417 {
418 int status;
419 struct inode *inode = NULL;
420 struct qstr *domain = &dentry->d_name;
421 struct dlmfs_inode_private *ip;
422 struct dlm_ctxt *dlm;
423
424 mlog(0, "mkdir %.*s\n", domain->len, domain->name);
425
426 /* verify that we have a proper domain */
427 if (domain->len >= O2NM_MAX_NAME_LEN) {
428 status = -EINVAL;
429 mlog(ML_ERROR, "invalid domain name for directory.\n");
430 goto bail;
431 }
432
433 inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
434 if (!inode) {
435 status = -ENOMEM;
436 mlog_errno(status);
437 goto bail;
438 }
439
440 ip = DLMFS_I(inode);
441
442 dlm = user_dlm_register_context(domain);
443 if (IS_ERR(dlm)) {
444 status = PTR_ERR(dlm);
445 mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
446 status, domain->len, domain->name);
447 goto bail;
448 }
449 ip->ip_dlm = dlm;
450
451 inc_nlink(dir);
452 d_instantiate(dentry, inode);
453 dget(dentry); /* Extra count - pin the dentry in core */
454
455 status = 0;
456 bail:
457 if (status < 0)
458 iput(inode);
459 return status;
460 }
461
462 static int dlmfs_create(struct inode *dir,
463 struct dentry *dentry,
464 int mode,
465 struct nameidata *nd)
466 {
467 int status = 0;
468 struct inode *inode;
469 struct qstr *name = &dentry->d_name;
470
471 mlog(0, "create %.*s\n", name->len, name->name);
472
473 /* verify name is valid and doesn't contain any dlm reserved
474 * characters */
475 if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
476 name->name[0] == '$') {
477 status = -EINVAL;
478 mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
479 name->name);
480 goto bail;
481 }
482
483 inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
484 if (!inode) {
485 status = -ENOMEM;
486 mlog_errno(status);
487 goto bail;
488 }
489
490 d_instantiate(dentry, inode);
491 dget(dentry); /* Extra count - pin the dentry in core */
492 bail:
493 return status;
494 }
495
496 static int dlmfs_unlink(struct inode *dir,
497 struct dentry *dentry)
498 {
499 int status;
500 struct inode *inode = dentry->d_inode;
501
502 mlog(0, "unlink inode %lu\n", inode->i_ino);
503
504 /* if there are no current holders, or none that are waiting
505 * to acquire a lock, this basically destroys our lockres. */
506 status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
507 if (status < 0) {
508 mlog(ML_ERROR, "unlink %.*s, error %d from destroy\n",
509 dentry->d_name.len, dentry->d_name.name, status);
510 goto bail;
511 }
512 status = simple_unlink(dir, dentry);
513 bail:
514 return status;
515 }
516
517 static int dlmfs_fill_super(struct super_block * sb,
518 void * data,
519 int silent)
520 {
521 struct inode * inode;
522 struct dentry * root;
523
524 sb->s_maxbytes = MAX_LFS_FILESIZE;
525 sb->s_blocksize = PAGE_CACHE_SIZE;
526 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
527 sb->s_magic = DLMFS_MAGIC;
528 sb->s_op = &dlmfs_ops;
529 inode = dlmfs_get_root_inode(sb);
530 if (!inode)
531 return -ENOMEM;
532
533 root = d_alloc_root(inode);
534 if (!root) {
535 iput(inode);
536 return -ENOMEM;
537 }
538 sb->s_root = root;
539 return 0;
540 }
541
542 static const struct file_operations dlmfs_file_operations = {
543 .open = dlmfs_file_open,
544 .release = dlmfs_file_release,
545 .read = dlmfs_file_read,
546 .write = dlmfs_file_write,
547 };
548
549 static const struct inode_operations dlmfs_dir_inode_operations = {
550 .create = dlmfs_create,
551 .lookup = simple_lookup,
552 .unlink = dlmfs_unlink,
553 };
554
555 /* this way we can restrict mkdir to only the toplevel of the fs. */
556 static const struct inode_operations dlmfs_root_inode_operations = {
557 .lookup = simple_lookup,
558 .mkdir = dlmfs_mkdir,
559 .rmdir = simple_rmdir,
560 };
561
562 static const struct super_operations dlmfs_ops = {
563 .statfs = simple_statfs,
564 .alloc_inode = dlmfs_alloc_inode,
565 .destroy_inode = dlmfs_destroy_inode,
566 .clear_inode = dlmfs_clear_inode,
567 .drop_inode = generic_delete_inode,
568 };
569
570 static const struct inode_operations dlmfs_file_inode_operations = {
571 .getattr = simple_getattr,
572 };
573
574 static int dlmfs_get_sb(struct file_system_type *fs_type,
575 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
576 {
577 return get_sb_nodev(fs_type, flags, data, dlmfs_fill_super, mnt);
578 }
579
580 static struct file_system_type dlmfs_fs_type = {
581 .owner = THIS_MODULE,
582 .name = "ocfs2_dlmfs",
583 .get_sb = dlmfs_get_sb,
584 .kill_sb = kill_litter_super,
585 };
586
587 static int __init init_dlmfs_fs(void)
588 {
589 int status;
590 int cleanup_inode = 0, cleanup_worker = 0;
591
592 dlmfs_print_version();
593
594 dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
595 sizeof(struct dlmfs_inode_private),
596 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
597 SLAB_MEM_SPREAD),
598 dlmfs_init_once, NULL);
599 if (!dlmfs_inode_cache)
600 return -ENOMEM;
601 cleanup_inode = 1;
602
603 user_dlm_worker = create_singlethread_workqueue("user_dlm");
604 if (!user_dlm_worker) {
605 status = -ENOMEM;
606 goto bail;
607 }
608 cleanup_worker = 1;
609
610 status = register_filesystem(&dlmfs_fs_type);
611 bail:
612 if (status) {
613 if (cleanup_inode)
614 kmem_cache_destroy(dlmfs_inode_cache);
615 if (cleanup_worker)
616 destroy_workqueue(user_dlm_worker);
617 } else
618 printk("OCFS2 User DLM kernel interface loaded\n");
619 return status;
620 }
621
622 static void __exit exit_dlmfs_fs(void)
623 {
624 unregister_filesystem(&dlmfs_fs_type);
625
626 flush_workqueue(user_dlm_worker);
627 destroy_workqueue(user_dlm_worker);
628
629 kmem_cache_destroy(dlmfs_inode_cache);
630 }
631
632 MODULE_AUTHOR("Oracle");
633 MODULE_LICENSE("GPL");
634
635 module_init(init_dlmfs_fs)
636 module_exit(exit_dlmfs_fs)