original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

709 lines
17 KiB

llseek: automatically add .llseek fop All file_operations should get a .llseek operation so we can make nonseekable_open the default for future file operations without a .llseek pointer. The three cases that we can automatically detect are no_llseek, seq_lseek and default_llseek. For cases where we can we can automatically prove that the file offset is always ignored, we use noop_llseek, which maintains the current behavior of not returning an error from a seek. New drivers should normally not use noop_llseek but instead use no_llseek and call nonseekable_open at open time. Existing drivers can be converted to do the same when the maintainer knows for certain that no user code relies on calling seek on the device file. The generated code is often incorrectly indented and right now contains comments that clarify for each added line why a specific variant was chosen. In the version that gets submitted upstream, the comments will be gone and I will manually fix the indentation, because there does not seem to be a way to do that using coccinelle. Some amount of new code is currently sitting in linux-next that should get the same modifications, which I will do at the end of the merge window. Many thanks to Julia Lawall for helping me learn to write a semantic patch that does all this. ===== begin semantic patch ===== // This adds an llseek= method to all file operations, // as a preparation for making no_llseek the default. // // The rules are // - use no_llseek explicitly if we do nonseekable_open // - use seq_lseek for sequential files // - use default_llseek if we know we access f_pos // - use noop_llseek if we know we don't access f_pos, // but we still want to allow users to call lseek // @ open1 exists @ identifier nested_open; @@ nested_open(...) { <+... nonseekable_open(...) ...+> } @ open exists@ identifier open_f; identifier i, f; identifier open1.nested_open; @@ int open_f(struct inode *i, struct file *f) { <+... ( nonseekable_open(...) | nested_open(...) ) ...+> } @ read disable optional_qualifier exists @ identifier read_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; expression E; identifier func; @@ ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off) { <+... ( *off = E | *off += E | func(..., off, ...) | E = *off ) ...+> } @ read_no_fpos disable optional_qualifier exists @ identifier read_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; @@ ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off) { ... when != off } @ write @ identifier write_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; expression E; identifier func; @@ ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off) { <+... ( *off = E | *off += E | func(..., off, ...) | E = *off ) ...+> } @ write_no_fpos @ identifier write_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; @@ ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off) { ... when != off } @ fops0 @ identifier fops; @@ struct file_operations fops = { ... }; @ has_llseek depends on fops0 @ identifier fops0.fops; identifier llseek_f; @@ struct file_operations fops = { ... .llseek = llseek_f, ... }; @ has_read depends on fops0 @ identifier fops0.fops; identifier read_f; @@ struct file_operations fops = { ... .read = read_f, ... }; @ has_write depends on fops0 @ identifier fops0.fops; identifier write_f; @@ struct file_operations fops = { ... .write = write_f, ... }; @ has_open depends on fops0 @ identifier fops0.fops; identifier open_f; @@ struct file_operations fops = { ... .open = open_f, ... }; // use no_llseek if we call nonseekable_open //////////////////////////////////////////// @ nonseekable1 depends on !has_llseek && has_open @ identifier fops0.fops; identifier nso ~= "nonseekable_open"; @@ struct file_operations fops = { ... .open = nso, ... +.llseek = no_llseek, /* nonseekable */ }; @ nonseekable2 depends on !has_llseek @ identifier fops0.fops; identifier open.open_f; @@ struct file_operations fops = { ... .open = open_f, ... +.llseek = no_llseek, /* open uses nonseekable */ }; // use seq_lseek for sequential files ///////////////////////////////////// @ seq depends on !has_llseek @ identifier fops0.fops; identifier sr ~= "seq_read"; @@ struct file_operations fops = { ... .read = sr, ... +.llseek = seq_lseek, /* we have seq_read */ }; // use default_llseek if there is a readdir /////////////////////////////////////////// @ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier readdir_e; @@ // any other fop is used that changes pos struct file_operations fops = { ... .readdir = readdir_e, ... +.llseek = default_llseek, /* readdir is present */ }; // use default_llseek if at least one of read/write touches f_pos ///////////////////////////////////////////////////////////////// @ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read.read_f; @@ // read fops use offset struct file_operations fops = { ... .read = read_f, ... +.llseek = default_llseek, /* read accesses f_pos */ }; @ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier write.write_f; @@ // write fops use offset struct file_operations fops = { ... .write = write_f, ... + .llseek = default_llseek, /* write accesses f_pos */ }; // Use noop_llseek if neither read nor write accesses f_pos /////////////////////////////////////////////////////////// @ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read_no_fpos.read_f; identifier write_no_fpos.write_f; @@ // write fops use offset struct file_operations fops = { ... .write = write_f, .read = read_f, ... +.llseek = noop_llseek, /* read and write both use no f_pos */ }; @ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier write_no_fpos.write_f; @@ struct file_operations fops = { ... .write = write_f, ... +.llseek = noop_llseek, /* write uses no f_pos */ }; @ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read_no_fpos.read_f; @@ struct file_operations fops = { ... .read = read_f, ... +.llseek = noop_llseek, /* read uses no f_pos */ }; @ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; @@ struct file_operations fops = { ... +.llseek = noop_llseek, /* no read or write fn */ }; ===== End semantic patch ===== Signed-off-by: Arnd Bergmann <arnd@arndb.de> Cc: Julia Lawall <julia@diku.dk> Cc: Christoph Hellwig <hch@infradead.org>
11 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  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. /* Simple VFS hooks based on: */
  29. /*
  30. * Resizable simple ram filesystem for Linux.
  31. *
  32. * Copyright (C) 2000 Linus Torvalds.
  33. * 2000 Transmeta Corp.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/types.h>
  39. #include <linux/slab.h>
  40. #include <linux/highmem.h>
  41. #include <linux/init.h>
  42. #include <linux/string.h>
  43. #include <linux/backing-dev.h>
  44. #include <linux/poll.h>
  45. #include <asm/uaccess.h>
  46. #include "stackglue.h"
  47. #include "userdlm.h"
  48. #include "dlmfsver.h"
  49. #define MLOG_MASK_PREFIX ML_DLMFS
  50. #include "cluster/masklog.h"
  51. static const struct super_operations dlmfs_ops;
  52. static const struct file_operations dlmfs_file_operations;
  53. static const struct inode_operations dlmfs_dir_inode_operations;
  54. static const struct inode_operations dlmfs_root_inode_operations;
  55. static const struct inode_operations dlmfs_file_inode_operations;
  56. static struct kmem_cache *dlmfs_inode_cache;
  57. struct workqueue_struct *user_dlm_worker;
  58. /*
  59. * These are the ABI capabilities of dlmfs.
  60. *
  61. * Over time, dlmfs has added some features that were not part of the
  62. * initial ABI. Unfortunately, some of these features are not detectable
  63. * via standard usage. For example, Linux's default poll always returns
  64. * POLLIN, so there is no way for a caller of poll(2) to know when dlmfs
  65. * added poll support. Instead, we provide this list of new capabilities.
  66. *
  67. * Capabilities is a read-only attribute. We do it as a module parameter
  68. * so we can discover it whether dlmfs is built in, loaded, or even not
  69. * loaded.
  70. *
  71. * The ABI features are local to this machine's dlmfs mount. This is
  72. * distinct from the locking protocol, which is concerned with inter-node
  73. * interaction.
  74. *
  75. * Capabilities:
  76. * - bast : POLLIN against the file descriptor of a held lock
  77. * signifies a bast fired on the lock.
  78. */
  79. #define DLMFS_CAPABILITIES "bast stackglue"
  80. static int param_set_dlmfs_capabilities(const char *val,
  81. struct kernel_param *kp)
  82. {
  83. printk(KERN_ERR "%s: readonly parameter\n", kp->name);
  84. return -EINVAL;
  85. }
  86. static int param_get_dlmfs_capabilities(char *buffer,
  87. struct kernel_param *kp)
  88. {
  89. return strlcpy(buffer, DLMFS_CAPABILITIES,
  90. strlen(DLMFS_CAPABILITIES) + 1);
  91. }
  92. module_param_call(capabilities, param_set_dlmfs_capabilities,
  93. param_get_dlmfs_capabilities, NULL, 0444);
  94. MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
  95. /*
  96. * decodes a set of open flags into a valid lock level and a set of flags.
  97. * returns < 0 if we have invalid flags
  98. * flags which mean something to us:
  99. * O_RDONLY -> PRMODE level
  100. * O_WRONLY -> EXMODE level
  101. *
  102. * O_NONBLOCK -> NOQUEUE
  103. */
  104. static int dlmfs_decode_open_flags(int open_flags,
  105. int *level,
  106. int *flags)
  107. {
  108. if (open_flags & (O_WRONLY|O_RDWR))
  109. *level = DLM_LOCK_EX;
  110. else
  111. *level = DLM_LOCK_PR;
  112. *flags = 0;
  113. if (open_flags & O_NONBLOCK)
  114. *flags |= DLM_LKF_NOQUEUE;
  115. return 0;
  116. }
  117. static int dlmfs_file_open(struct inode *inode,
  118. struct file *file)
  119. {
  120. int status, level, flags;
  121. struct dlmfs_filp_private *fp = NULL;
  122. struct dlmfs_inode_private *ip;
  123. if (S_ISDIR(inode->i_mode))
  124. BUG();
  125. mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
  126. file->f_flags);
  127. status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
  128. if (status < 0)
  129. goto bail;
  130. /* We don't want to honor O_APPEND at read/write time as it
  131. * doesn't make sense for LVB writes. */
  132. file->f_flags &= ~O_APPEND;
  133. fp = kmalloc(sizeof(*fp), GFP_NOFS);
  134. if (!fp) {
  135. status = -ENOMEM;
  136. goto bail;
  137. }
  138. fp->fp_lock_level = level;
  139. ip = DLMFS_I(inode);
  140. status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
  141. if (status < 0) {
  142. /* this is a strange error to return here but I want
  143. * to be able userspace to be able to distinguish a
  144. * valid lock request from one that simply couldn't be
  145. * granted. */
  146. if (flags & DLM_LKF_NOQUEUE && status == -EAGAIN)
  147. status = -ETXTBSY;
  148. kfree(fp);
  149. goto bail;
  150. }
  151. file->private_data = fp;
  152. bail:
  153. return status;
  154. }
  155. static int dlmfs_file_release(struct inode *inode,
  156. struct file *file)
  157. {
  158. int level, status;
  159. struct dlmfs_inode_private *ip = DLMFS_I(inode);
  160. struct dlmfs_filp_private *fp = file->private_data;
  161. if (S_ISDIR(inode->i_mode))
  162. BUG();
  163. mlog(0, "close called on inode %lu\n", inode->i_ino);
  164. status = 0;
  165. if (fp) {
  166. level = fp->fp_lock_level;
  167. if (level != DLM_LOCK_IV)
  168. user_dlm_cluster_unlock(&ip->ip_lockres, level);
  169. kfree(fp);
  170. file->private_data = NULL;
  171. }
  172. return 0;
  173. }
  174. /*
  175. * We do ->setattr() just to override size changes. Our size is the size
  176. * of the LVB and nothing else.
  177. */
  178. static int dlmfs_file_setattr(struct dentry *dentry, struct iattr *attr)
  179. {
  180. int error;
  181. struct inode *inode = dentry->d_inode;
  182. attr->ia_valid &= ~ATTR_SIZE;
  183. error = inode_change_ok(inode, attr);
  184. if (error)
  185. return error;
  186. setattr_copy(inode, attr);
  187. mark_inode_dirty(inode);
  188. return 0;
  189. }
  190. static unsigned int dlmfs_file_poll(struct file *file, poll_table *wait)
  191. {
  192. int event = 0;
  193. struct inode *inode = file_inode(file);
  194. struct dlmfs_inode_private *ip = DLMFS_I(inode);
  195. poll_wait(file, &ip->ip_lockres.l_event, wait);
  196. spin_lock(&ip->ip_lockres.l_lock);
  197. if (ip->ip_lockres.l_flags & USER_LOCK_BLOCKED)
  198. event = POLLIN | POLLRDNORM;
  199. spin_unlock(&ip->ip_lockres.l_lock);
  200. return event;
  201. }
  202. static ssize_t dlmfs_file_read(struct file *filp,
  203. char __user *buf,
  204. size_t count,
  205. loff_t *ppos)
  206. {
  207. int bytes_left;
  208. ssize_t readlen, got;
  209. char *lvb_buf;
  210. struct inode *inode = file_inode(filp);
  211. mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
  212. inode->i_ino, count, *ppos);
  213. if (*ppos >= i_size_read(inode))
  214. return 0;
  215. if (!count)
  216. return 0;
  217. if (!access_ok(VERIFY_WRITE, buf, count))
  218. return -EFAULT;
  219. /* don't read past the lvb */
  220. if ((count + *ppos) > i_size_read(inode))
  221. readlen = i_size_read(inode) - *ppos;
  222. else
  223. readlen = count;
  224. lvb_buf = kmalloc(readlen, GFP_NOFS);
  225. if (!lvb_buf)
  226. return -ENOMEM;
  227. got = user_dlm_read_lvb(inode, lvb_buf, readlen);
  228. if (got) {
  229. BUG_ON(got != readlen);
  230. bytes_left = __copy_to_user(buf, lvb_buf, readlen);
  231. readlen -= bytes_left;
  232. } else
  233. readlen = 0;
  234. kfree(lvb_buf);
  235. *ppos = *ppos + readlen;
  236. mlog(0, "read %zd bytes\n", readlen);
  237. return readlen;
  238. }
  239. static ssize_t dlmfs_file_write(struct file *filp,
  240. const char __user *buf,
  241. size_t count,
  242. loff_t *ppos)
  243. {
  244. int bytes_left;
  245. ssize_t writelen;
  246. char *lvb_buf;
  247. struct inode *inode = file_inode(filp);
  248. mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
  249. inode->i_ino, count, *ppos);
  250. if (*ppos >= i_size_read(inode))
  251. return -ENOSPC;
  252. if (!count)
  253. return 0;
  254. if (!access_ok(VERIFY_READ, buf, count))
  255. return -EFAULT;
  256. /* don't write past the lvb */
  257. if ((count + *ppos) > i_size_read(inode))
  258. writelen = i_size_read(inode) - *ppos;
  259. else
  260. writelen = count - *ppos;
  261. lvb_buf = kmalloc(writelen, GFP_NOFS);
  262. if (!lvb_buf)
  263. return -ENOMEM;
  264. bytes_left = copy_from_user(lvb_buf, buf, writelen);
  265. writelen -= bytes_left;
  266. if (writelen)
  267. user_dlm_write_lvb(inode, lvb_buf, writelen);
  268. kfree(lvb_buf);
  269. *ppos = *ppos + writelen;
  270. mlog(0, "wrote %zd bytes\n", writelen);
  271. return writelen;
  272. }
  273. static void dlmfs_init_once(void *foo)
  274. {
  275. struct dlmfs_inode_private *ip =
  276. (struct dlmfs_inode_private *) foo;
  277. ip->ip_conn = NULL;
  278. ip->ip_parent = NULL;
  279. inode_init_once(&ip->ip_vfs_inode);
  280. }
  281. static struct inode *dlmfs_alloc_inode(struct super_block *sb)
  282. {
  283. struct dlmfs_inode_private *ip;
  284. ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
  285. if (!ip)
  286. return NULL;
  287. return &ip->ip_vfs_inode;
  288. }
  289. static void dlmfs_i_callback(struct rcu_head *head)
  290. {
  291. struct inode *inode = container_of(head, struct inode, i_rcu);
  292. kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
  293. }
  294. static void dlmfs_destroy_inode(struct inode *inode)
  295. {
  296. call_rcu(&inode->i_rcu, dlmfs_i_callback);
  297. }
  298. static void dlmfs_evict_inode(struct inode *inode)
  299. {
  300. int status;
  301. struct dlmfs_inode_private *ip;
  302. clear_inode(inode);
  303. mlog(0, "inode %lu\n", inode->i_ino);
  304. ip = DLMFS_I(inode);
  305. if (S_ISREG(inode->i_mode)) {
  306. status = user_dlm_destroy_lock(&ip->ip_lockres);
  307. if (status < 0)
  308. mlog_errno(status);
  309. iput(ip->ip_parent);
  310. goto clear_fields;
  311. }
  312. mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
  313. /* we must be a directory. If required, lets unregister the
  314. * dlm context now. */
  315. if (ip->ip_conn)
  316. user_dlm_unregister(ip->ip_conn);
  317. clear_fields:
  318. ip->ip_parent = NULL;
  319. ip->ip_conn = NULL;
  320. }
  321. static struct backing_dev_info dlmfs_backing_dev_info = {
  322. .name = "ocfs2-dlmfs",
  323. .ra_pages = 0, /* No readahead */
  324. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  325. };
  326. static struct inode *dlmfs_get_root_inode(struct super_block *sb)
  327. {
  328. struct inode *inode = new_inode(sb);
  329. umode_t mode = S_IFDIR | 0755;
  330. struct dlmfs_inode_private *ip;
  331. if (inode) {
  332. ip = DLMFS_I(inode);
  333. inode->i_ino = get_next_ino();
  334. inode_init_owner(inode, NULL, mode);
  335. inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
  336. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  337. inc_nlink(inode);
  338. inode->i_fop = &simple_dir_operations;
  339. inode->i_op = &dlmfs_root_inode_operations;
  340. }
  341. return inode;
  342. }
  343. static struct inode *dlmfs_get_inode(struct inode *parent,
  344. struct dentry *dentry,
  345. umode_t mode)
  346. {
  347. struct super_block *sb = parent->i_sb;
  348. struct inode * inode = new_inode(sb);
  349. struct dlmfs_inode_private *ip;
  350. if (!inode)
  351. return NULL;
  352. inode->i_ino = get_next_ino();
  353. inode_init_owner(inode, parent, mode);
  354. inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
  355. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  356. ip = DLMFS_I(inode);
  357. ip->ip_conn = DLMFS_I(parent)->ip_conn;
  358. switch (mode & S_IFMT) {
  359. default:
  360. /* for now we don't support anything other than
  361. * directories and regular files. */
  362. BUG();
  363. break;
  364. case S_IFREG:
  365. inode->i_op = &dlmfs_file_inode_operations;
  366. inode->i_fop = &dlmfs_file_operations;
  367. i_size_write(inode, DLM_LVB_LEN);
  368. user_dlm_lock_res_init(&ip->ip_lockres, dentry);
  369. /* released at clear_inode time, this insures that we
  370. * get to drop the dlm reference on each lock *before*
  371. * we call the unregister code for releasing parent
  372. * directories. */
  373. ip->ip_parent = igrab(parent);
  374. BUG_ON(!ip->ip_parent);
  375. break;
  376. case S_IFDIR:
  377. inode->i_op = &dlmfs_dir_inode_operations;
  378. inode->i_fop = &simple_dir_operations;
  379. /* directory inodes start off with i_nlink ==
  380. * 2 (for "." entry) */
  381. inc_nlink(inode);
  382. break;
  383. }
  384. return inode;
  385. }
  386. /*
  387. * File creation. Allocate an inode, and we're done..
  388. */
  389. /* SMP-safe */
  390. static int dlmfs_mkdir(struct inode * dir,
  391. struct dentry * dentry,
  392. umode_t mode)
  393. {
  394. int status;
  395. struct inode *inode = NULL;
  396. struct qstr *domain = &dentry->d_name;
  397. struct dlmfs_inode_private *ip;
  398. struct ocfs2_cluster_connection *conn;
  399. mlog(0, "mkdir %.*s\n", domain->len, domain->name);
  400. /* verify that we have a proper domain */
  401. if (domain->len >= GROUP_NAME_MAX) {
  402. status = -EINVAL;
  403. mlog(ML_ERROR, "invalid domain name for directory.\n");
  404. goto bail;
  405. }
  406. inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
  407. if (!inode) {
  408. status = -ENOMEM;
  409. mlog_errno(status);
  410. goto bail;
  411. }
  412. ip = DLMFS_I(inode);
  413. conn = user_dlm_register(domain);
  414. if (IS_ERR(conn)) {
  415. status = PTR_ERR(conn);
  416. mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
  417. status, domain->len, domain->name);
  418. goto bail;
  419. }
  420. ip->ip_conn = conn;
  421. inc_nlink(dir);
  422. d_instantiate(dentry, inode);
  423. dget(dentry); /* Extra count - pin the dentry in core */
  424. status = 0;
  425. bail:
  426. if (status < 0)
  427. iput(inode);
  428. return status;
  429. }
  430. static int dlmfs_create(struct inode *dir,
  431. struct dentry *dentry,
  432. umode_t mode,
  433. bool excl)
  434. {
  435. int status = 0;
  436. struct inode *inode;
  437. struct qstr *name = &dentry->d_name;
  438. mlog(0, "create %.*s\n", name->len, name->name);
  439. /* verify name is valid and doesn't contain any dlm reserved
  440. * characters */
  441. if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
  442. name->name[0] == '$') {
  443. status = -EINVAL;
  444. mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
  445. name->name);
  446. goto bail;
  447. }
  448. inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
  449. if (!inode) {
  450. status = -ENOMEM;
  451. mlog_errno(status);
  452. goto bail;
  453. }
  454. d_instantiate(dentry, inode);
  455. dget(dentry); /* Extra count - pin the dentry in core */
  456. bail:
  457. return status;
  458. }
  459. static int dlmfs_unlink(struct inode *dir,
  460. struct dentry *dentry)
  461. {
  462. int status;
  463. struct inode *inode = dentry->d_inode;
  464. mlog(0, "unlink inode %lu\n", inode->i_ino);
  465. /* if there are no current holders, or none that are waiting
  466. * to acquire a lock, this basically destroys our lockres. */
  467. status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
  468. if (status < 0) {
  469. mlog(ML_ERROR, "unlink %.*s, error %d from destroy\n",
  470. dentry->d_name.len, dentry->d_name.name, status);
  471. goto bail;
  472. }
  473. status = simple_unlink(dir, dentry);
  474. bail:
  475. return status;
  476. }
  477. static int dlmfs_fill_super(struct super_block * sb,
  478. void * data,
  479. int silent)
  480. {
  481. sb->s_maxbytes = MAX_LFS_FILESIZE;
  482. sb->s_blocksize = PAGE_CACHE_SIZE;
  483. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  484. sb->s_magic = DLMFS_MAGIC;
  485. sb->s_op = &dlmfs_ops;
  486. sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
  487. if (!sb->s_root)
  488. return -ENOMEM;
  489. return 0;
  490. }
  491. static const struct file_operations dlmfs_file_operations = {
  492. .open = dlmfs_file_open,
  493. .release = dlmfs_file_release,
  494. .poll = dlmfs_file_poll,
  495. .read = dlmfs_file_read,
  496. .write = dlmfs_file_write,
  497. .llseek = default_llseek,
  498. };
  499. static const struct inode_operations dlmfs_dir_inode_operations = {
  500. .create = dlmfs_create,
  501. .lookup = simple_lookup,
  502. .unlink = dlmfs_unlink,
  503. };
  504. /* this way we can restrict mkdir to only the toplevel of the fs. */
  505. static const struct inode_operations dlmfs_root_inode_operations = {
  506. .lookup = simple_lookup,
  507. .mkdir = dlmfs_mkdir,
  508. .rmdir = simple_rmdir,
  509. };
  510. static const struct super_operations dlmfs_ops = {
  511. .statfs = simple_statfs,
  512. .alloc_inode = dlmfs_alloc_inode,
  513. .destroy_inode = dlmfs_destroy_inode,
  514. .evict_inode = dlmfs_evict_inode,
  515. .drop_inode = generic_delete_inode,
  516. };
  517. static const struct inode_operations dlmfs_file_inode_operations = {
  518. .getattr = simple_getattr,
  519. .setattr = dlmfs_file_setattr,
  520. };
  521. static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
  522. int flags, const char *dev_name, void *data)
  523. {
  524. return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
  525. }
  526. static struct file_system_type dlmfs_fs_type = {
  527. .owner = THIS_MODULE,
  528. .name = "ocfs2_dlmfs",
  529. .mount = dlmfs_mount,
  530. .kill_sb = kill_litter_super,
  531. };
  532. MODULE_ALIAS_FS("ocfs2_dlmfs");
  533. static int __init init_dlmfs_fs(void)
  534. {
  535. int status;
  536. int cleanup_inode = 0, cleanup_worker = 0;
  537. dlmfs_print_version();
  538. status = bdi_init(&dlmfs_backing_dev_info);
  539. if (status)
  540. return status;
  541. dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
  542. sizeof(struct dlmfs_inode_private),
  543. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  544. SLAB_MEM_SPREAD),
  545. dlmfs_init_once);
  546. if (!dlmfs_inode_cache) {
  547. status = -ENOMEM;
  548. goto bail;
  549. }
  550. cleanup_inode = 1;
  551. user_dlm_worker = create_singlethread_workqueue("user_dlm");
  552. if (!user_dlm_worker) {
  553. status = -ENOMEM;
  554. goto bail;
  555. }
  556. cleanup_worker = 1;
  557. user_dlm_set_locking_protocol();
  558. status = register_filesystem(&dlmfs_fs_type);
  559. bail:
  560. if (status) {
  561. if (cleanup_inode)
  562. kmem_cache_destroy(dlmfs_inode_cache);
  563. if (cleanup_worker)
  564. destroy_workqueue(user_dlm_worker);
  565. bdi_destroy(&dlmfs_backing_dev_info);
  566. } else
  567. printk("OCFS2 User DLM kernel interface loaded\n");
  568. return status;
  569. }
  570. static void __exit exit_dlmfs_fs(void)
  571. {
  572. unregister_filesystem(&dlmfs_fs_type);
  573. flush_workqueue(user_dlm_worker);
  574. destroy_workqueue(user_dlm_worker);
  575. /*
  576. * Make sure all delayed rcu free inodes are flushed before we
  577. * destroy cache.
  578. */
  579. rcu_barrier();
  580. kmem_cache_destroy(dlmfs_inode_cache);
  581. bdi_destroy(&dlmfs_backing_dev_info);
  582. }
  583. MODULE_AUTHOR("Oracle");
  584. MODULE_LICENSE("GPL");
  585. module_init(init_dlmfs_fs)
  586. module_exit(exit_dlmfs_fs)