1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * dlmfs.c
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Code which implements the kernel side of a minimal userspace
8*4882a593Smuzhiyun * interface to our DLM. This file handles the virtual file system
9*4882a593Smuzhiyun * used for communication with userspace. Credit should go to ramfs,
10*4882a593Smuzhiyun * which was a template for the fs side of this module.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Copyright (C) 2003, 2004 Oracle. All rights reserved.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /* Simple VFS hooks based on: */
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun * Resizable simple ram filesystem for Linux.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Copyright (C) 2000 Linus Torvalds.
20*4882a593Smuzhiyun * 2000 Transmeta Corp.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/fs.h>
25*4882a593Smuzhiyun #include <linux/pagemap.h>
26*4882a593Smuzhiyun #include <linux/types.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/highmem.h>
29*4882a593Smuzhiyun #include <linux/init.h>
30*4882a593Smuzhiyun #include <linux/string.h>
31*4882a593Smuzhiyun #include <linux/backing-dev.h>
32*4882a593Smuzhiyun #include <linux/poll.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include <linux/uaccess.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include "../stackglue.h"
37*4882a593Smuzhiyun #include "userdlm.h"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define MLOG_MASK_PREFIX ML_DLMFS
40*4882a593Smuzhiyun #include "../cluster/masklog.h"
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun static const struct super_operations dlmfs_ops;
44*4882a593Smuzhiyun static const struct file_operations dlmfs_file_operations;
45*4882a593Smuzhiyun static const struct inode_operations dlmfs_dir_inode_operations;
46*4882a593Smuzhiyun static const struct inode_operations dlmfs_root_inode_operations;
47*4882a593Smuzhiyun static const struct inode_operations dlmfs_file_inode_operations;
48*4882a593Smuzhiyun static struct kmem_cache *dlmfs_inode_cache;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun struct workqueue_struct *user_dlm_worker;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * These are the ABI capabilities of dlmfs.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * Over time, dlmfs has added some features that were not part of the
58*4882a593Smuzhiyun * initial ABI. Unfortunately, some of these features are not detectable
59*4882a593Smuzhiyun * via standard usage. For example, Linux's default poll always returns
60*4882a593Smuzhiyun * EPOLLIN, so there is no way for a caller of poll(2) to know when dlmfs
61*4882a593Smuzhiyun * added poll support. Instead, we provide this list of new capabilities.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Capabilities is a read-only attribute. We do it as a module parameter
64*4882a593Smuzhiyun * so we can discover it whether dlmfs is built in, loaded, or even not
65*4882a593Smuzhiyun * loaded.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * The ABI features are local to this machine's dlmfs mount. This is
68*4882a593Smuzhiyun * distinct from the locking protocol, which is concerned with inter-node
69*4882a593Smuzhiyun * interaction.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * Capabilities:
72*4882a593Smuzhiyun * - bast : EPOLLIN against the file descriptor of a held lock
73*4882a593Smuzhiyun * signifies a bast fired on the lock.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #define DLMFS_CAPABILITIES "bast stackglue"
param_set_dlmfs_capabilities(const char * val,const struct kernel_param * kp)76*4882a593Smuzhiyun static int param_set_dlmfs_capabilities(const char *val,
77*4882a593Smuzhiyun const struct kernel_param *kp)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun printk(KERN_ERR "%s: readonly parameter\n", kp->name);
80*4882a593Smuzhiyun return -EINVAL;
81*4882a593Smuzhiyun }
param_get_dlmfs_capabilities(char * buffer,const struct kernel_param * kp)82*4882a593Smuzhiyun static int param_get_dlmfs_capabilities(char *buffer,
83*4882a593Smuzhiyun const struct kernel_param *kp)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun return strlcpy(buffer, DLMFS_CAPABILITIES,
86*4882a593Smuzhiyun strlen(DLMFS_CAPABILITIES) + 1);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun module_param_call(capabilities, param_set_dlmfs_capabilities,
89*4882a593Smuzhiyun param_get_dlmfs_capabilities, NULL, 0444);
90*4882a593Smuzhiyun MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * decodes a set of open flags into a valid lock level and a set of flags.
95*4882a593Smuzhiyun * returns < 0 if we have invalid flags
96*4882a593Smuzhiyun * flags which mean something to us:
97*4882a593Smuzhiyun * O_RDONLY -> PRMODE level
98*4882a593Smuzhiyun * O_WRONLY -> EXMODE level
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * O_NONBLOCK -> NOQUEUE
101*4882a593Smuzhiyun */
dlmfs_decode_open_flags(int open_flags,int * level,int * flags)102*4882a593Smuzhiyun static int dlmfs_decode_open_flags(int open_flags,
103*4882a593Smuzhiyun int *level,
104*4882a593Smuzhiyun int *flags)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun if (open_flags & (O_WRONLY|O_RDWR))
107*4882a593Smuzhiyun *level = DLM_LOCK_EX;
108*4882a593Smuzhiyun else
109*4882a593Smuzhiyun *level = DLM_LOCK_PR;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun *flags = 0;
112*4882a593Smuzhiyun if (open_flags & O_NONBLOCK)
113*4882a593Smuzhiyun *flags |= DLM_LKF_NOQUEUE;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
dlmfs_file_open(struct inode * inode,struct file * file)118*4882a593Smuzhiyun static int dlmfs_file_open(struct inode *inode,
119*4882a593Smuzhiyun struct file *file)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun int status, level, flags;
122*4882a593Smuzhiyun struct dlmfs_filp_private *fp = NULL;
123*4882a593Smuzhiyun struct dlmfs_inode_private *ip;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode))
126*4882a593Smuzhiyun BUG();
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
129*4882a593Smuzhiyun file->f_flags);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
132*4882a593Smuzhiyun if (status < 0)
133*4882a593Smuzhiyun goto bail;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* We don't want to honor O_APPEND at read/write time as it
136*4882a593Smuzhiyun * doesn't make sense for LVB writes. */
137*4882a593Smuzhiyun file->f_flags &= ~O_APPEND;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun fp = kmalloc(sizeof(*fp), GFP_NOFS);
140*4882a593Smuzhiyun if (!fp) {
141*4882a593Smuzhiyun status = -ENOMEM;
142*4882a593Smuzhiyun goto bail;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun fp->fp_lock_level = level;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun ip = DLMFS_I(inode);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
149*4882a593Smuzhiyun if (status < 0) {
150*4882a593Smuzhiyun /* this is a strange error to return here but I want
151*4882a593Smuzhiyun * to be able userspace to be able to distinguish a
152*4882a593Smuzhiyun * valid lock request from one that simply couldn't be
153*4882a593Smuzhiyun * granted. */
154*4882a593Smuzhiyun if (flags & DLM_LKF_NOQUEUE && status == -EAGAIN)
155*4882a593Smuzhiyun status = -ETXTBSY;
156*4882a593Smuzhiyun kfree(fp);
157*4882a593Smuzhiyun goto bail;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun file->private_data = fp;
161*4882a593Smuzhiyun bail:
162*4882a593Smuzhiyun return status;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
dlmfs_file_release(struct inode * inode,struct file * file)165*4882a593Smuzhiyun static int dlmfs_file_release(struct inode *inode,
166*4882a593Smuzhiyun struct file *file)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun int level;
169*4882a593Smuzhiyun struct dlmfs_inode_private *ip = DLMFS_I(inode);
170*4882a593Smuzhiyun struct dlmfs_filp_private *fp = file->private_data;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode))
173*4882a593Smuzhiyun BUG();
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun mlog(0, "close called on inode %lu\n", inode->i_ino);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (fp) {
178*4882a593Smuzhiyun level = fp->fp_lock_level;
179*4882a593Smuzhiyun if (level != DLM_LOCK_IV)
180*4882a593Smuzhiyun user_dlm_cluster_unlock(&ip->ip_lockres, level);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun kfree(fp);
183*4882a593Smuzhiyun file->private_data = NULL;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return 0;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * We do ->setattr() just to override size changes. Our size is the size
191*4882a593Smuzhiyun * of the LVB and nothing else.
192*4882a593Smuzhiyun */
dlmfs_file_setattr(struct dentry * dentry,struct iattr * attr)193*4882a593Smuzhiyun static int dlmfs_file_setattr(struct dentry *dentry, struct iattr *attr)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun int error;
196*4882a593Smuzhiyun struct inode *inode = d_inode(dentry);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun attr->ia_valid &= ~ATTR_SIZE;
199*4882a593Smuzhiyun error = setattr_prepare(dentry, attr);
200*4882a593Smuzhiyun if (error)
201*4882a593Smuzhiyun return error;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun setattr_copy(inode, attr);
204*4882a593Smuzhiyun mark_inode_dirty(inode);
205*4882a593Smuzhiyun return 0;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
dlmfs_file_poll(struct file * file,poll_table * wait)208*4882a593Smuzhiyun static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun __poll_t event = 0;
211*4882a593Smuzhiyun struct inode *inode = file_inode(file);
212*4882a593Smuzhiyun struct dlmfs_inode_private *ip = DLMFS_I(inode);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun poll_wait(file, &ip->ip_lockres.l_event, wait);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun spin_lock(&ip->ip_lockres.l_lock);
217*4882a593Smuzhiyun if (ip->ip_lockres.l_flags & USER_LOCK_BLOCKED)
218*4882a593Smuzhiyun event = EPOLLIN | EPOLLRDNORM;
219*4882a593Smuzhiyun spin_unlock(&ip->ip_lockres.l_lock);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun return event;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
dlmfs_file_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)224*4882a593Smuzhiyun static ssize_t dlmfs_file_read(struct file *file,
225*4882a593Smuzhiyun char __user *buf,
226*4882a593Smuzhiyun size_t count,
227*4882a593Smuzhiyun loff_t *ppos)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun char lvb[DLM_LVB_LEN];
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun if (!user_dlm_read_lvb(file_inode(file), lvb))
232*4882a593Smuzhiyun return 0;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun return simple_read_from_buffer(buf, count, ppos, lvb, sizeof(lvb));
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
dlmfs_file_write(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)237*4882a593Smuzhiyun static ssize_t dlmfs_file_write(struct file *filp,
238*4882a593Smuzhiyun const char __user *buf,
239*4882a593Smuzhiyun size_t count,
240*4882a593Smuzhiyun loff_t *ppos)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun char lvb_buf[DLM_LVB_LEN];
243*4882a593Smuzhiyun int bytes_left;
244*4882a593Smuzhiyun struct inode *inode = file_inode(filp);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
247*4882a593Smuzhiyun inode->i_ino, count, *ppos);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (*ppos >= DLM_LVB_LEN)
250*4882a593Smuzhiyun return -ENOSPC;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* don't write past the lvb */
253*4882a593Smuzhiyun if (count > DLM_LVB_LEN - *ppos)
254*4882a593Smuzhiyun count = DLM_LVB_LEN - *ppos;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun if (!count)
257*4882a593Smuzhiyun return 0;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun bytes_left = copy_from_user(lvb_buf, buf, count);
260*4882a593Smuzhiyun count -= bytes_left;
261*4882a593Smuzhiyun if (count)
262*4882a593Smuzhiyun user_dlm_write_lvb(inode, lvb_buf, count);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun *ppos = *ppos + count;
265*4882a593Smuzhiyun mlog(0, "wrote %zu bytes\n", count);
266*4882a593Smuzhiyun return count;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
dlmfs_init_once(void * foo)269*4882a593Smuzhiyun static void dlmfs_init_once(void *foo)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun struct dlmfs_inode_private *ip =
272*4882a593Smuzhiyun (struct dlmfs_inode_private *) foo;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun ip->ip_conn = NULL;
275*4882a593Smuzhiyun ip->ip_parent = NULL;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun inode_init_once(&ip->ip_vfs_inode);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
dlmfs_alloc_inode(struct super_block * sb)280*4882a593Smuzhiyun static struct inode *dlmfs_alloc_inode(struct super_block *sb)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun struct dlmfs_inode_private *ip;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
285*4882a593Smuzhiyun if (!ip)
286*4882a593Smuzhiyun return NULL;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun return &ip->ip_vfs_inode;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
dlmfs_free_inode(struct inode * inode)291*4882a593Smuzhiyun static void dlmfs_free_inode(struct inode *inode)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
dlmfs_evict_inode(struct inode * inode)296*4882a593Smuzhiyun static void dlmfs_evict_inode(struct inode *inode)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun int status;
299*4882a593Smuzhiyun struct dlmfs_inode_private *ip;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun clear_inode(inode);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun mlog(0, "inode %lu\n", inode->i_ino);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun ip = DLMFS_I(inode);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (S_ISREG(inode->i_mode)) {
308*4882a593Smuzhiyun status = user_dlm_destroy_lock(&ip->ip_lockres);
309*4882a593Smuzhiyun if (status < 0)
310*4882a593Smuzhiyun mlog_errno(status);
311*4882a593Smuzhiyun iput(ip->ip_parent);
312*4882a593Smuzhiyun goto clear_fields;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
316*4882a593Smuzhiyun /* we must be a directory. If required, lets unregister the
317*4882a593Smuzhiyun * dlm context now. */
318*4882a593Smuzhiyun if (ip->ip_conn)
319*4882a593Smuzhiyun user_dlm_unregister(ip->ip_conn);
320*4882a593Smuzhiyun clear_fields:
321*4882a593Smuzhiyun ip->ip_parent = NULL;
322*4882a593Smuzhiyun ip->ip_conn = NULL;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
dlmfs_get_root_inode(struct super_block * sb)325*4882a593Smuzhiyun static struct inode *dlmfs_get_root_inode(struct super_block *sb)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct inode *inode = new_inode(sb);
328*4882a593Smuzhiyun umode_t mode = S_IFDIR | 0755;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun if (inode) {
331*4882a593Smuzhiyun inode->i_ino = get_next_ino();
332*4882a593Smuzhiyun inode_init_owner(inode, NULL, mode);
333*4882a593Smuzhiyun inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
334*4882a593Smuzhiyun inc_nlink(inode);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun inode->i_fop = &simple_dir_operations;
337*4882a593Smuzhiyun inode->i_op = &dlmfs_root_inode_operations;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return inode;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
dlmfs_get_inode(struct inode * parent,struct dentry * dentry,umode_t mode)343*4882a593Smuzhiyun static struct inode *dlmfs_get_inode(struct inode *parent,
344*4882a593Smuzhiyun struct dentry *dentry,
345*4882a593Smuzhiyun umode_t mode)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun struct super_block *sb = parent->i_sb;
348*4882a593Smuzhiyun struct inode * inode = new_inode(sb);
349*4882a593Smuzhiyun struct dlmfs_inode_private *ip;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun if (!inode)
352*4882a593Smuzhiyun return NULL;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun inode->i_ino = get_next_ino();
355*4882a593Smuzhiyun inode_init_owner(inode, parent, mode);
356*4882a593Smuzhiyun inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun ip = DLMFS_I(inode);
359*4882a593Smuzhiyun ip->ip_conn = DLMFS_I(parent)->ip_conn;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun switch (mode & S_IFMT) {
362*4882a593Smuzhiyun default:
363*4882a593Smuzhiyun /* for now we don't support anything other than
364*4882a593Smuzhiyun * directories and regular files. */
365*4882a593Smuzhiyun BUG();
366*4882a593Smuzhiyun break;
367*4882a593Smuzhiyun case S_IFREG:
368*4882a593Smuzhiyun inode->i_op = &dlmfs_file_inode_operations;
369*4882a593Smuzhiyun inode->i_fop = &dlmfs_file_operations;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun i_size_write(inode, DLM_LVB_LEN);
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun user_dlm_lock_res_init(&ip->ip_lockres, dentry);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun /* released at clear_inode time, this insures that we
376*4882a593Smuzhiyun * get to drop the dlm reference on each lock *before*
377*4882a593Smuzhiyun * we call the unregister code for releasing parent
378*4882a593Smuzhiyun * directories. */
379*4882a593Smuzhiyun ip->ip_parent = igrab(parent);
380*4882a593Smuzhiyun BUG_ON(!ip->ip_parent);
381*4882a593Smuzhiyun break;
382*4882a593Smuzhiyun case S_IFDIR:
383*4882a593Smuzhiyun inode->i_op = &dlmfs_dir_inode_operations;
384*4882a593Smuzhiyun inode->i_fop = &simple_dir_operations;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* directory inodes start off with i_nlink ==
387*4882a593Smuzhiyun * 2 (for "." entry) */
388*4882a593Smuzhiyun inc_nlink(inode);
389*4882a593Smuzhiyun break;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun return inode;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun * File creation. Allocate an inode, and we're done..
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun /* SMP-safe */
dlmfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)398*4882a593Smuzhiyun static int dlmfs_mkdir(struct inode * dir,
399*4882a593Smuzhiyun struct dentry * dentry,
400*4882a593Smuzhiyun umode_t mode)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun int status;
403*4882a593Smuzhiyun struct inode *inode = NULL;
404*4882a593Smuzhiyun const struct qstr *domain = &dentry->d_name;
405*4882a593Smuzhiyun struct dlmfs_inode_private *ip;
406*4882a593Smuzhiyun struct ocfs2_cluster_connection *conn;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun mlog(0, "mkdir %.*s\n", domain->len, domain->name);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* verify that we have a proper domain */
411*4882a593Smuzhiyun if (domain->len >= GROUP_NAME_MAX) {
412*4882a593Smuzhiyun status = -EINVAL;
413*4882a593Smuzhiyun mlog(ML_ERROR, "invalid domain name for directory.\n");
414*4882a593Smuzhiyun goto bail;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
418*4882a593Smuzhiyun if (!inode) {
419*4882a593Smuzhiyun status = -ENOMEM;
420*4882a593Smuzhiyun mlog_errno(status);
421*4882a593Smuzhiyun goto bail;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun ip = DLMFS_I(inode);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun conn = user_dlm_register(domain);
427*4882a593Smuzhiyun if (IS_ERR(conn)) {
428*4882a593Smuzhiyun status = PTR_ERR(conn);
429*4882a593Smuzhiyun mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
430*4882a593Smuzhiyun status, domain->len, domain->name);
431*4882a593Smuzhiyun goto bail;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun ip->ip_conn = conn;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun inc_nlink(dir);
436*4882a593Smuzhiyun d_instantiate(dentry, inode);
437*4882a593Smuzhiyun dget(dentry); /* Extra count - pin the dentry in core */
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun status = 0;
440*4882a593Smuzhiyun bail:
441*4882a593Smuzhiyun if (status < 0)
442*4882a593Smuzhiyun iput(inode);
443*4882a593Smuzhiyun return status;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
dlmfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)446*4882a593Smuzhiyun static int dlmfs_create(struct inode *dir,
447*4882a593Smuzhiyun struct dentry *dentry,
448*4882a593Smuzhiyun umode_t mode,
449*4882a593Smuzhiyun bool excl)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun int status = 0;
452*4882a593Smuzhiyun struct inode *inode;
453*4882a593Smuzhiyun const struct qstr *name = &dentry->d_name;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun mlog(0, "create %.*s\n", name->len, name->name);
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /* verify name is valid and doesn't contain any dlm reserved
458*4882a593Smuzhiyun * characters */
459*4882a593Smuzhiyun if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
460*4882a593Smuzhiyun name->name[0] == '$') {
461*4882a593Smuzhiyun status = -EINVAL;
462*4882a593Smuzhiyun mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
463*4882a593Smuzhiyun name->name);
464*4882a593Smuzhiyun goto bail;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
468*4882a593Smuzhiyun if (!inode) {
469*4882a593Smuzhiyun status = -ENOMEM;
470*4882a593Smuzhiyun mlog_errno(status);
471*4882a593Smuzhiyun goto bail;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun d_instantiate(dentry, inode);
475*4882a593Smuzhiyun dget(dentry); /* Extra count - pin the dentry in core */
476*4882a593Smuzhiyun bail:
477*4882a593Smuzhiyun return status;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
dlmfs_unlink(struct inode * dir,struct dentry * dentry)480*4882a593Smuzhiyun static int dlmfs_unlink(struct inode *dir,
481*4882a593Smuzhiyun struct dentry *dentry)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun int status;
484*4882a593Smuzhiyun struct inode *inode = d_inode(dentry);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun mlog(0, "unlink inode %lu\n", inode->i_ino);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /* if there are no current holders, or none that are waiting
489*4882a593Smuzhiyun * to acquire a lock, this basically destroys our lockres. */
490*4882a593Smuzhiyun status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
491*4882a593Smuzhiyun if (status < 0) {
492*4882a593Smuzhiyun mlog(ML_ERROR, "unlink %pd, error %d from destroy\n",
493*4882a593Smuzhiyun dentry, status);
494*4882a593Smuzhiyun goto bail;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun status = simple_unlink(dir, dentry);
497*4882a593Smuzhiyun bail:
498*4882a593Smuzhiyun return status;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
dlmfs_fill_super(struct super_block * sb,void * data,int silent)501*4882a593Smuzhiyun static int dlmfs_fill_super(struct super_block * sb,
502*4882a593Smuzhiyun void * data,
503*4882a593Smuzhiyun int silent)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun sb->s_maxbytes = MAX_LFS_FILESIZE;
506*4882a593Smuzhiyun sb->s_blocksize = PAGE_SIZE;
507*4882a593Smuzhiyun sb->s_blocksize_bits = PAGE_SHIFT;
508*4882a593Smuzhiyun sb->s_magic = DLMFS_MAGIC;
509*4882a593Smuzhiyun sb->s_op = &dlmfs_ops;
510*4882a593Smuzhiyun sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
511*4882a593Smuzhiyun if (!sb->s_root)
512*4882a593Smuzhiyun return -ENOMEM;
513*4882a593Smuzhiyun return 0;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun static const struct file_operations dlmfs_file_operations = {
517*4882a593Smuzhiyun .open = dlmfs_file_open,
518*4882a593Smuzhiyun .release = dlmfs_file_release,
519*4882a593Smuzhiyun .poll = dlmfs_file_poll,
520*4882a593Smuzhiyun .read = dlmfs_file_read,
521*4882a593Smuzhiyun .write = dlmfs_file_write,
522*4882a593Smuzhiyun .llseek = default_llseek,
523*4882a593Smuzhiyun };
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun static const struct inode_operations dlmfs_dir_inode_operations = {
526*4882a593Smuzhiyun .create = dlmfs_create,
527*4882a593Smuzhiyun .lookup = simple_lookup,
528*4882a593Smuzhiyun .unlink = dlmfs_unlink,
529*4882a593Smuzhiyun };
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun /* this way we can restrict mkdir to only the toplevel of the fs. */
532*4882a593Smuzhiyun static const struct inode_operations dlmfs_root_inode_operations = {
533*4882a593Smuzhiyun .lookup = simple_lookup,
534*4882a593Smuzhiyun .mkdir = dlmfs_mkdir,
535*4882a593Smuzhiyun .rmdir = simple_rmdir,
536*4882a593Smuzhiyun };
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun static const struct super_operations dlmfs_ops = {
539*4882a593Smuzhiyun .statfs = simple_statfs,
540*4882a593Smuzhiyun .alloc_inode = dlmfs_alloc_inode,
541*4882a593Smuzhiyun .free_inode = dlmfs_free_inode,
542*4882a593Smuzhiyun .evict_inode = dlmfs_evict_inode,
543*4882a593Smuzhiyun .drop_inode = generic_delete_inode,
544*4882a593Smuzhiyun };
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun static const struct inode_operations dlmfs_file_inode_operations = {
547*4882a593Smuzhiyun .getattr = simple_getattr,
548*4882a593Smuzhiyun .setattr = dlmfs_file_setattr,
549*4882a593Smuzhiyun };
550*4882a593Smuzhiyun
dlmfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)551*4882a593Smuzhiyun static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
552*4882a593Smuzhiyun int flags, const char *dev_name, void *data)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun static struct file_system_type dlmfs_fs_type = {
558*4882a593Smuzhiyun .owner = THIS_MODULE,
559*4882a593Smuzhiyun .name = "ocfs2_dlmfs",
560*4882a593Smuzhiyun .mount = dlmfs_mount,
561*4882a593Smuzhiyun .kill_sb = kill_litter_super,
562*4882a593Smuzhiyun };
563*4882a593Smuzhiyun MODULE_ALIAS_FS("ocfs2_dlmfs");
564*4882a593Smuzhiyun
init_dlmfs_fs(void)565*4882a593Smuzhiyun static int __init init_dlmfs_fs(void)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun int status;
568*4882a593Smuzhiyun int cleanup_inode = 0, cleanup_worker = 0;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
571*4882a593Smuzhiyun sizeof(struct dlmfs_inode_private),
572*4882a593Smuzhiyun 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
573*4882a593Smuzhiyun SLAB_MEM_SPREAD|SLAB_ACCOUNT),
574*4882a593Smuzhiyun dlmfs_init_once);
575*4882a593Smuzhiyun if (!dlmfs_inode_cache) {
576*4882a593Smuzhiyun status = -ENOMEM;
577*4882a593Smuzhiyun goto bail;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun cleanup_inode = 1;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0);
582*4882a593Smuzhiyun if (!user_dlm_worker) {
583*4882a593Smuzhiyun status = -ENOMEM;
584*4882a593Smuzhiyun goto bail;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun cleanup_worker = 1;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun user_dlm_set_locking_protocol();
589*4882a593Smuzhiyun status = register_filesystem(&dlmfs_fs_type);
590*4882a593Smuzhiyun bail:
591*4882a593Smuzhiyun if (status) {
592*4882a593Smuzhiyun if (cleanup_inode)
593*4882a593Smuzhiyun kmem_cache_destroy(dlmfs_inode_cache);
594*4882a593Smuzhiyun if (cleanup_worker)
595*4882a593Smuzhiyun destroy_workqueue(user_dlm_worker);
596*4882a593Smuzhiyun } else
597*4882a593Smuzhiyun printk("OCFS2 User DLM kernel interface loaded\n");
598*4882a593Smuzhiyun return status;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
exit_dlmfs_fs(void)601*4882a593Smuzhiyun static void __exit exit_dlmfs_fs(void)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun unregister_filesystem(&dlmfs_fs_type);
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun destroy_workqueue(user_dlm_worker);
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * Make sure all delayed rcu free inodes are flushed before we
609*4882a593Smuzhiyun * destroy cache.
610*4882a593Smuzhiyun */
611*4882a593Smuzhiyun rcu_barrier();
612*4882a593Smuzhiyun kmem_cache_destroy(dlmfs_inode_cache);
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun MODULE_AUTHOR("Oracle");
617*4882a593Smuzhiyun MODULE_LICENSE("GPL");
618*4882a593Smuzhiyun MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
619*4882a593Smuzhiyun MODULE_DESCRIPTION("OCFS2 DLM-Filesystem");
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun module_init(init_dlmfs_fs)
622*4882a593Smuzhiyun module_exit(exit_dlmfs_fs)
623