dlmfs.c 15 KB

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