[PATCH] mark struct inode_operations const 3
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ocfs2 / dlm / dlmfs.c
CommitLineData
8df08c89
MF
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
64static struct super_operations dlmfs_ops;
00977a59 65static const struct file_operations dlmfs_file_operations;
92e1d5be
AV
66static const struct inode_operations dlmfs_dir_inode_operations;
67static const struct inode_operations dlmfs_root_inode_operations;
68static const struct inode_operations dlmfs_file_inode_operations;
e18b890b 69static struct kmem_cache *dlmfs_inode_cache;
8df08c89
MF
70
71struct 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 */
82static 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
98static 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
ad8100e0 119 fp = kmalloc(sizeof(*fp), GFP_NOFS);
8df08c89
MF
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;
141bail:
142 return status;
143}
144
145static 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
171static 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;
d28c9174 179 struct inode *inode = filp->f_path.dentry->d_inode;
8df08c89
MF
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
ad8100e0 199 lvb_buf = kmalloc(readlen, GFP_NOFS);
8df08c89
MF
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
215static 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;
d28c9174 223 struct inode *inode = filp->f_path.dentry->d_inode;
8df08c89
MF
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
ad8100e0 243 lvb_buf = kmalloc(writelen, GFP_NOFS);
8df08c89
MF
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
259static void dlmfs_init_once(void *foo,
e18b890b 260 struct kmem_cache *cachep,
8df08c89
MF
261 unsigned long flags)
262{
263 struct dlmfs_inode_private *ip =
264 (struct dlmfs_inode_private *) foo;
265
266 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
267 SLAB_CTOR_CONSTRUCTOR) {
268 ip->ip_dlm = NULL;
269 ip->ip_parent = NULL;
270
271 inode_init_once(&ip->ip_vfs_inode);
272 }
273}
274
275static struct inode *dlmfs_alloc_inode(struct super_block *sb)
276{
277 struct dlmfs_inode_private *ip;
278
e6b4f8da 279 ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
8df08c89
MF
280 if (!ip)
281 return NULL;
282
283 return &ip->ip_vfs_inode;
284}
285
286static void dlmfs_destroy_inode(struct inode *inode)
287{
288 kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
289}
290
291static void dlmfs_clear_inode(struct inode *inode)
292{
293 int status;
294 struct dlmfs_inode_private *ip;
295
296 if (!inode)
297 return;
298
299 mlog(0, "inode %lu\n", inode->i_ino);
300
301 ip = DLMFS_I(inode);
302
303 if (S_ISREG(inode->i_mode)) {
304 status = user_dlm_destroy_lock(&ip->ip_lockres);
305 if (status < 0)
306 mlog_errno(status);
307 iput(ip->ip_parent);
308 goto clear_fields;
309 }
310
311 mlog(0, "we're a directory, ip->ip_dlm = 0x%p\n", ip->ip_dlm);
312 /* we must be a directory. If required, lets unregister the
313 * dlm context now. */
314 if (ip->ip_dlm)
315 user_dlm_unregister_context(ip->ip_dlm);
316clear_fields:
317 ip->ip_parent = NULL;
318 ip->ip_dlm = NULL;
319}
320
321static struct backing_dev_info dlmfs_backing_dev_info = {
322 .ra_pages = 0, /* No readahead */
323 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
324};
325
326static struct inode *dlmfs_get_root_inode(struct super_block *sb)
327{
328 struct inode *inode = new_inode(sb);
329 int mode = S_IFDIR | 0755;
330 struct dlmfs_inode_private *ip;
331
332 if (inode) {
333 ip = DLMFS_I(inode);
334
335 inode->i_mode = mode;
336 inode->i_uid = current->fsuid;
337 inode->i_gid = current->fsgid;
8df08c89
MF
338 inode->i_blocks = 0;
339 inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
340 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
d8c76e6f 341 inc_nlink(inode);
8df08c89
MF
342
343 inode->i_fop = &simple_dir_operations;
344 inode->i_op = &dlmfs_root_inode_operations;
345 }
346
347 return inode;
348}
349
350static struct inode *dlmfs_get_inode(struct inode *parent,
351 struct dentry *dentry,
352 int mode)
353{
354 struct super_block *sb = parent->i_sb;
355 struct inode * inode = new_inode(sb);
356 struct dlmfs_inode_private *ip;
357
358 if (!inode)
359 return NULL;
360
361 inode->i_mode = mode;
362 inode->i_uid = current->fsuid;
363 inode->i_gid = current->fsgid;
8df08c89
MF
364 inode->i_blocks = 0;
365 inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
366 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
367
368 ip = DLMFS_I(inode);
369 ip->ip_dlm = DLMFS_I(parent)->ip_dlm;
370
371 switch (mode & S_IFMT) {
372 default:
373 /* for now we don't support anything other than
374 * directories and regular files. */
375 BUG();
376 break;
377 case S_IFREG:
378 inode->i_op = &dlmfs_file_inode_operations;
379 inode->i_fop = &dlmfs_file_operations;
380
381 i_size_write(inode, DLM_LVB_LEN);
382
383 user_dlm_lock_res_init(&ip->ip_lockres, dentry);
384
385 /* released at clear_inode time, this insures that we
386 * get to drop the dlm reference on each lock *before*
387 * we call the unregister code for releasing parent
388 * directories. */
389 ip->ip_parent = igrab(parent);
390 BUG_ON(!ip->ip_parent);
391 break;
392 case S_IFDIR:
393 inode->i_op = &dlmfs_dir_inode_operations;
394 inode->i_fop = &simple_dir_operations;
395
396 /* directory inodes start off with i_nlink ==
397 * 2 (for "." entry) */
d8c76e6f 398 inc_nlink(inode);
8df08c89
MF
399 break;
400 }
401
402 if (parent->i_mode & S_ISGID) {
403 inode->i_gid = parent->i_gid;
404 if (S_ISDIR(mode))
405 inode->i_mode |= S_ISGID;
406 }
407
408 return inode;
409}
410
411/*
412 * File creation. Allocate an inode, and we're done..
413 */
414/* SMP-safe */
415static int dlmfs_mkdir(struct inode * dir,
416 struct dentry * dentry,
417 int mode)
418{
419 int status;
420 struct inode *inode = NULL;
421 struct qstr *domain = &dentry->d_name;
422 struct dlmfs_inode_private *ip;
423 struct dlm_ctxt *dlm;
424
425 mlog(0, "mkdir %.*s\n", domain->len, domain->name);
426
427 /* verify that we have a proper domain */
428 if (domain->len >= O2NM_MAX_NAME_LEN) {
429 status = -EINVAL;
430 mlog(ML_ERROR, "invalid domain name for directory.\n");
431 goto bail;
432 }
433
434 inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
435 if (!inode) {
436 status = -ENOMEM;
437 mlog_errno(status);
438 goto bail;
439 }
440
441 ip = DLMFS_I(inode);
442
443 dlm = user_dlm_register_context(domain);
444 if (IS_ERR(dlm)) {
445 status = PTR_ERR(dlm);
446 mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
447 status, domain->len, domain->name);
448 goto bail;
449 }
450 ip->ip_dlm = dlm;
451
d8c76e6f 452 inc_nlink(dir);
8df08c89
MF
453 d_instantiate(dentry, inode);
454 dget(dentry); /* Extra count - pin the dentry in core */
455
456 status = 0;
457bail:
458 if (status < 0)
459 iput(inode);
460 return status;
461}
462
463static int dlmfs_create(struct inode *dir,
464 struct dentry *dentry,
465 int mode,
466 struct nameidata *nd)
467{
468 int status = 0;
469 struct inode *inode;
470 struct qstr *name = &dentry->d_name;
471
472 mlog(0, "create %.*s\n", name->len, name->name);
473
474 /* verify name is valid and doesn't contain any dlm reserved
475 * characters */
476 if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
477 name->name[0] == '$') {
478 status = -EINVAL;
479 mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
480 name->name);
481 goto bail;
482 }
483
484 inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
485 if (!inode) {
486 status = -ENOMEM;
487 mlog_errno(status);
488 goto bail;
489 }
490
491 d_instantiate(dentry, inode);
492 dget(dentry); /* Extra count - pin the dentry in core */
493bail:
494 return status;
495}
496
497static int dlmfs_unlink(struct inode *dir,
498 struct dentry *dentry)
499{
500 int status;
501 struct inode *inode = dentry->d_inode;
502
503 mlog(0, "unlink inode %lu\n", inode->i_ino);
504
505 /* if there are no current holders, or none that are waiting
506 * to acquire a lock, this basically destroys our lockres. */
507 status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
508 if (status < 0) {
509 mlog(ML_ERROR, "unlink %.*s, error %d from destroy\n",
510 dentry->d_name.len, dentry->d_name.name, status);
511 goto bail;
512 }
513 status = simple_unlink(dir, dentry);
514bail:
515 return status;
516}
517
518static int dlmfs_fill_super(struct super_block * sb,
519 void * data,
520 int silent)
521{
522 struct inode * inode;
523 struct dentry * root;
524
525 sb->s_maxbytes = MAX_LFS_FILESIZE;
526 sb->s_blocksize = PAGE_CACHE_SIZE;
527 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
528 sb->s_magic = DLMFS_MAGIC;
529 sb->s_op = &dlmfs_ops;
530 inode = dlmfs_get_root_inode(sb);
531 if (!inode)
532 return -ENOMEM;
533
534 root = d_alloc_root(inode);
535 if (!root) {
536 iput(inode);
537 return -ENOMEM;
538 }
539 sb->s_root = root;
540 return 0;
541}
542
00977a59 543static const struct file_operations dlmfs_file_operations = {
8df08c89
MF
544 .open = dlmfs_file_open,
545 .release = dlmfs_file_release,
546 .read = dlmfs_file_read,
547 .write = dlmfs_file_write,
548};
549
92e1d5be 550static const struct inode_operations dlmfs_dir_inode_operations = {
8df08c89
MF
551 .create = dlmfs_create,
552 .lookup = simple_lookup,
553 .unlink = dlmfs_unlink,
554};
555
556/* this way we can restrict mkdir to only the toplevel of the fs. */
92e1d5be 557static const struct inode_operations dlmfs_root_inode_operations = {
8df08c89
MF
558 .lookup = simple_lookup,
559 .mkdir = dlmfs_mkdir,
560 .rmdir = simple_rmdir,
561};
562
563static struct super_operations dlmfs_ops = {
564 .statfs = simple_statfs,
565 .alloc_inode = dlmfs_alloc_inode,
566 .destroy_inode = dlmfs_destroy_inode,
567 .clear_inode = dlmfs_clear_inode,
568 .drop_inode = generic_delete_inode,
569};
570
92e1d5be 571static const struct inode_operations dlmfs_file_inode_operations = {
8df08c89
MF
572 .getattr = simple_getattr,
573};
574
454e2398
DH
575static int dlmfs_get_sb(struct file_system_type *fs_type,
576 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
8df08c89 577{
454e2398 578 return get_sb_nodev(fs_type, flags, data, dlmfs_fill_super, mnt);
8df08c89
MF
579}
580
581static struct file_system_type dlmfs_fs_type = {
582 .owner = THIS_MODULE,
583 .name = "ocfs2_dlmfs",
584 .get_sb = dlmfs_get_sb,
585 .kill_sb = kill_litter_super,
586};
587
588static int __init init_dlmfs_fs(void)
589{
590 int status;
591 int cleanup_inode = 0, cleanup_worker = 0;
592
593 dlmfs_print_version();
594
595 dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
596 sizeof(struct dlmfs_inode_private),
fffb60f9
PJ
597 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
598 SLAB_MEM_SPREAD),
8df08c89
MF
599 dlmfs_init_once, NULL);
600 if (!dlmfs_inode_cache)
601 return -ENOMEM;
602 cleanup_inode = 1;
603
604 user_dlm_worker = create_singlethread_workqueue("user_dlm");
605 if (!user_dlm_worker) {
606 status = -ENOMEM;
607 goto bail;
608 }
609 cleanup_worker = 1;
610
611 status = register_filesystem(&dlmfs_fs_type);
612bail:
613 if (status) {
614 if (cleanup_inode)
615 kmem_cache_destroy(dlmfs_inode_cache);
616 if (cleanup_worker)
617 destroy_workqueue(user_dlm_worker);
618 } else
619 printk("OCFS2 User DLM kernel interface loaded\n");
620 return status;
621}
622
623static void __exit exit_dlmfs_fs(void)
624{
625 unregister_filesystem(&dlmfs_fs_type);
626
627 flush_workqueue(user_dlm_worker);
628 destroy_workqueue(user_dlm_worker);
629
1a1d92c1 630 kmem_cache_destroy(dlmfs_inode_cache);
8df08c89
MF
631}
632
633MODULE_AUTHOR("Oracle");
634MODULE_LICENSE("GPL");
635
636module_init(init_dlmfs_fs)
637module_exit(exit_dlmfs_fs)