dlmfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 <asm/uaccess.h>
  45. #include "cluster/nodemanager.h"
  46. #include "cluster/heartbeat.h"
  47. #include "cluster/tcp.h"
  48. #include "dlmapi.h"
  49. #include "userdlm.h"
  50. #include "dlmfsver.h"
  51. #define MLOG_MASK_PREFIX ML_DLMFS
  52. #include "cluster/masklog.h"
  53. static const struct super_operations dlmfs_ops;
  54. static const struct file_operations dlmfs_file_operations;
  55. static const struct inode_operations dlmfs_dir_inode_operations;
  56. static const struct inode_operations dlmfs_root_inode_operations;
  57. static const struct inode_operations dlmfs_file_inode_operations;
  58. static struct kmem_cache *dlmfs_inode_cache;
  59. struct workqueue_struct *user_dlm_worker;
  60. /*
  61. * decodes a set of open flags into a valid lock level and a set of flags.
  62. * returns < 0 if we have invalid flags
  63. * flags which mean something to us:
  64. * O_RDONLY -> PRMODE level
  65. * O_WRONLY -> EXMODE level
  66. *
  67. * O_NONBLOCK -> LKM_NOQUEUE
  68. */
  69. static int dlmfs_decode_open_flags(int open_flags,
  70. int *level,
  71. int *flags)
  72. {
  73. if (open_flags & (O_WRONLY|O_RDWR))
  74. *level = LKM_EXMODE;
  75. else
  76. *level = LKM_PRMODE;
  77. *flags = 0;
  78. if (open_flags & O_NONBLOCK)
  79. *flags |= LKM_NOQUEUE;
  80. return 0;
  81. }
  82. static int dlmfs_file_open(struct inode *inode,
  83. struct file *file)
  84. {
  85. int status, level, flags;
  86. struct dlmfs_filp_private *fp = NULL;
  87. struct dlmfs_inode_private *ip;
  88. if (S_ISDIR(inode->i_mode))
  89. BUG();
  90. mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
  91. file->f_flags);
  92. status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
  93. if (status < 0)
  94. goto bail;
  95. /* We don't want to honor O_APPEND at read/write time as it
  96. * doesn't make sense for LVB writes. */
  97. file->f_flags &= ~O_APPEND;
  98. fp = kmalloc(sizeof(*fp), GFP_NOFS);
  99. if (!fp) {
  100. status = -ENOMEM;
  101. goto bail;
  102. }
  103. fp->fp_lock_level = level;
  104. ip = DLMFS_I(inode);
  105. status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
  106. if (status < 0) {
  107. /* this is a strange error to return here but I want
  108. * to be able userspace to be able to distinguish a
  109. * valid lock request from one that simply couldn't be
  110. * granted. */
  111. if (flags & LKM_NOQUEUE && status == -EAGAIN)
  112. status = -ETXTBSY;
  113. kfree(fp);
  114. goto bail;
  115. }
  116. file->private_data = fp;
  117. bail:
  118. return status;
  119. }
  120. static int dlmfs_file_release(struct inode *inode,
  121. struct file *file)
  122. {
  123. int level, status;
  124. struct dlmfs_inode_private *ip = DLMFS_I(inode);
  125. struct dlmfs_filp_private *fp =
  126. (struct dlmfs_filp_private *) file->private_data;
  127. if (S_ISDIR(inode->i_mode))
  128. BUG();
  129. mlog(0, "close called on inode %lu\n", inode->i_ino);
  130. status = 0;
  131. if (fp) {
  132. level = fp->fp_lock_level;
  133. if (level != LKM_IVMODE)
  134. user_dlm_cluster_unlock(&ip->ip_lockres, level);
  135. kfree(fp);
  136. file->private_data = NULL;
  137. }
  138. return 0;
  139. }
  140. static ssize_t dlmfs_file_read(struct file *filp,
  141. char __user *buf,
  142. size_t count,
  143. loff_t *ppos)
  144. {
  145. int bytes_left;
  146. ssize_t readlen;
  147. char *lvb_buf;
  148. struct inode *inode = filp->f_path.dentry->d_inode;
  149. mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
  150. inode->i_ino, count, *ppos);
  151. if (*ppos >= i_size_read(inode))
  152. return 0;
  153. if (!count)
  154. return 0;
  155. if (!access_ok(VERIFY_WRITE, buf, count))
  156. return -EFAULT;
  157. /* don't read past the lvb */
  158. if ((count + *ppos) > i_size_read(inode))
  159. readlen = i_size_read(inode) - *ppos;
  160. else
  161. readlen = count - *ppos;
  162. lvb_buf = kmalloc(readlen, GFP_NOFS);
  163. if (!lvb_buf)
  164. return -ENOMEM;
  165. user_dlm_read_lvb(inode, lvb_buf, readlen);
  166. bytes_left = __copy_to_user(buf, lvb_buf, readlen);
  167. readlen -= bytes_left;
  168. kfree(lvb_buf);
  169. *ppos = *ppos + readlen;
  170. mlog(0, "read %zd bytes\n", readlen);
  171. return readlen;
  172. }
  173. static ssize_t dlmfs_file_write(struct file *filp,
  174. const char __user *buf,
  175. size_t count,
  176. loff_t *ppos)
  177. {
  178. int bytes_left;
  179. ssize_t writelen;
  180. char *lvb_buf;
  181. struct inode *inode = filp->f_path.dentry->d_inode;
  182. mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
  183. inode->i_ino, count, *ppos);
  184. if (*ppos >= i_size_read(inode))
  185. return -ENOSPC;
  186. if (!count)
  187. return 0;
  188. if (!access_ok(VERIFY_READ, buf, count))
  189. return -EFAULT;
  190. /* don't write past the lvb */
  191. if ((count + *ppos) > i_size_read(inode))
  192. writelen = i_size_read(inode) - *ppos;
  193. else
  194. writelen = count - *ppos;
  195. lvb_buf = kmalloc(writelen, GFP_NOFS);
  196. if (!lvb_buf)
  197. return -ENOMEM;
  198. bytes_left = copy_from_user(lvb_buf, buf, writelen);
  199. writelen -= bytes_left;
  200. if (writelen)
  201. user_dlm_write_lvb(inode, lvb_buf, writelen);
  202. kfree(lvb_buf);
  203. *ppos = *ppos + writelen;
  204. mlog(0, "wrote %zd bytes\n", writelen);
  205. return writelen;
  206. }
  207. static void dlmfs_init_once(void *foo,
  208. struct kmem_cache *cachep,
  209. unsigned long flags)
  210. {
  211. struct dlmfs_inode_private *ip =
  212. (struct dlmfs_inode_private *) foo;
  213. if (flags & SLAB_CTOR_CONSTRUCTOR) {
  214. ip->ip_dlm = NULL;
  215. ip->ip_parent = NULL;
  216. inode_init_once(&ip->ip_vfs_inode);
  217. }
  218. }
  219. static struct inode *dlmfs_alloc_inode(struct super_block *sb)
  220. {
  221. struct dlmfs_inode_private *ip;
  222. ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
  223. if (!ip)
  224. return NULL;
  225. return &ip->ip_vfs_inode;
  226. }
  227. static void dlmfs_destroy_inode(struct inode *inode)
  228. {
  229. kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
  230. }
  231. static void dlmfs_clear_inode(struct inode *inode)
  232. {
  233. int status;
  234. struct dlmfs_inode_private *ip;
  235. if (!inode)
  236. return;
  237. mlog(0, "inode %lu\n", inode->i_ino);
  238. ip = DLMFS_I(inode);
  239. if (S_ISREG(inode->i_mode)) {
  240. status = user_dlm_destroy_lock(&ip->ip_lockres);
  241. if (status < 0)
  242. mlog_errno(status);
  243. iput(ip->ip_parent);
  244. goto clear_fields;
  245. }
  246. mlog(0, "we're a directory, ip->ip_dlm = 0x%p\n", ip->ip_dlm);
  247. /* we must be a directory. If required, lets unregister the
  248. * dlm context now. */
  249. if (ip->ip_dlm)
  250. user_dlm_unregister_context(ip->ip_dlm);
  251. clear_fields:
  252. ip->ip_parent = NULL;
  253. ip->ip_dlm = NULL;
  254. }
  255. static struct backing_dev_info dlmfs_backing_dev_info = {
  256. .ra_pages = 0, /* No readahead */
  257. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  258. };
  259. static struct inode *dlmfs_get_root_inode(struct super_block *sb)
  260. {
  261. struct inode *inode = new_inode(sb);
  262. int mode = S_IFDIR | 0755;
  263. struct dlmfs_inode_private *ip;
  264. if (inode) {
  265. ip = DLMFS_I(inode);
  266. inode->i_mode = mode;
  267. inode->i_uid = current->fsuid;
  268. inode->i_gid = current->fsgid;
  269. inode->i_blocks = 0;
  270. inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
  271. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  272. inc_nlink(inode);
  273. inode->i_fop = &simple_dir_operations;
  274. inode->i_op = &dlmfs_root_inode_operations;
  275. }
  276. return inode;
  277. }
  278. static struct inode *dlmfs_get_inode(struct inode *parent,
  279. struct dentry *dentry,
  280. int mode)
  281. {
  282. struct super_block *sb = parent->i_sb;
  283. struct inode * inode = new_inode(sb);
  284. struct dlmfs_inode_private *ip;
  285. if (!inode)
  286. return NULL;
  287. inode->i_mode = mode;
  288. inode->i_uid = current->fsuid;
  289. inode->i_gid = current->fsgid;
  290. inode->i_blocks = 0;
  291. inode->i_mapping->backing_dev_info = &dlmfs_backing_dev_info;
  292. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  293. ip = DLMFS_I(inode);
  294. ip->ip_dlm = DLMFS_I(parent)->ip_dlm;
  295. switch (mode & S_IFMT) {
  296. default:
  297. /* for now we don't support anything other than
  298. * directories and regular files. */
  299. BUG();
  300. break;
  301. case S_IFREG:
  302. inode->i_op = &dlmfs_file_inode_operations;
  303. inode->i_fop = &dlmfs_file_operations;
  304. i_size_write(inode, DLM_LVB_LEN);
  305. user_dlm_lock_res_init(&ip->ip_lockres, dentry);
  306. /* released at clear_inode time, this insures that we
  307. * get to drop the dlm reference on each lock *before*
  308. * we call the unregister code for releasing parent
  309. * directories. */
  310. ip->ip_parent = igrab(parent);
  311. BUG_ON(!ip->ip_parent);
  312. break;
  313. case S_IFDIR:
  314. inode->i_op = &dlmfs_dir_inode_operations;
  315. inode->i_fop = &simple_dir_operations;
  316. /* directory inodes start off with i_nlink ==
  317. * 2 (for "." entry) */
  318. inc_nlink(inode);
  319. break;
  320. }
  321. if (parent->i_mode & S_ISGID) {
  322. inode->i_gid = parent->i_gid;
  323. if (S_ISDIR(mode))
  324. inode->i_mode |= S_ISGID;
  325. }
  326. return inode;
  327. }
  328. /*
  329. * File creation. Allocate an inode, and we're done..
  330. */
  331. /* SMP-safe */
  332. static int dlmfs_mkdir(struct inode * dir,
  333. struct dentry * dentry,
  334. int mode)
  335. {
  336. int status;
  337. struct inode *inode = NULL;
  338. struct qstr *domain = &dentry->d_name;
  339. struct dlmfs_inode_private *ip;
  340. struct dlm_ctxt *dlm;
  341. mlog(0, "mkdir %.*s\n", domain->len, domain->name);
  342. /* verify that we have a proper domain */
  343. if (domain->len >= O2NM_MAX_NAME_LEN) {
  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. dlm = user_dlm_register_context(domain);
  356. if (IS_ERR(dlm)) {
  357. status = PTR_ERR(dlm);
  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_dlm = dlm;
  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 inode *dir,
  373. struct dentry *dentry,
  374. int mode,
  375. struct nameidata *nd)
  376. {
  377. int status = 0;
  378. struct inode *inode;
  379. struct qstr *name = &dentry->d_name;
  380. mlog(0, "create %.*s\n", name->len, name->name);
  381. /* verify name is valid and doesn't contain any dlm reserved
  382. * characters */
  383. if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
  384. name->name[0] == '$') {
  385. status = -EINVAL;
  386. mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
  387. name->name);
  388. goto bail;
  389. }
  390. inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
  391. if (!inode) {
  392. status = -ENOMEM;
  393. mlog_errno(status);
  394. goto bail;
  395. }
  396. d_instantiate(dentry, inode);
  397. dget(dentry); /* Extra count - pin the dentry in core */
  398. bail:
  399. return status;
  400. }
  401. static int dlmfs_unlink(struct inode *dir,
  402. struct dentry *dentry)
  403. {
  404. int status;
  405. struct inode *inode = dentry->d_inode;
  406. mlog(0, "unlink inode %lu\n", inode->i_ino);
  407. /* if there are no current holders, or none that are waiting
  408. * to acquire a lock, this basically destroys our lockres. */
  409. status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
  410. if (status < 0) {
  411. mlog(ML_ERROR, "unlink %.*s, error %d from destroy\n",
  412. dentry->d_name.len, dentry->d_name.name, status);
  413. goto bail;
  414. }
  415. status = simple_unlink(dir, dentry);
  416. bail:
  417. return status;
  418. }
  419. static int dlmfs_fill_super(struct super_block * sb,
  420. void * data,
  421. int silent)
  422. {
  423. struct inode * inode;
  424. struct dentry * root;
  425. sb->s_maxbytes = MAX_LFS_FILESIZE;
  426. sb->s_blocksize = PAGE_CACHE_SIZE;
  427. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  428. sb->s_magic = DLMFS_MAGIC;
  429. sb->s_op = &dlmfs_ops;
  430. inode = dlmfs_get_root_inode(sb);
  431. if (!inode)
  432. return -ENOMEM;
  433. root = d_alloc_root(inode);
  434. if (!root) {
  435. iput(inode);
  436. return -ENOMEM;
  437. }
  438. sb->s_root = root;
  439. return 0;
  440. }
  441. static const struct file_operations dlmfs_file_operations = {
  442. .open = dlmfs_file_open,
  443. .release = dlmfs_file_release,
  444. .read = dlmfs_file_read,
  445. .write = dlmfs_file_write,
  446. };
  447. static const struct inode_operations dlmfs_dir_inode_operations = {
  448. .create = dlmfs_create,
  449. .lookup = simple_lookup,
  450. .unlink = dlmfs_unlink,
  451. };
  452. /* this way we can restrict mkdir to only the toplevel of the fs. */
  453. static const struct inode_operations dlmfs_root_inode_operations = {
  454. .lookup = simple_lookup,
  455. .mkdir = dlmfs_mkdir,
  456. .rmdir = simple_rmdir,
  457. };
  458. static const struct super_operations dlmfs_ops = {
  459. .statfs = simple_statfs,
  460. .alloc_inode = dlmfs_alloc_inode,
  461. .destroy_inode = dlmfs_destroy_inode,
  462. .clear_inode = dlmfs_clear_inode,
  463. .drop_inode = generic_delete_inode,
  464. };
  465. static const struct inode_operations dlmfs_file_inode_operations = {
  466. .getattr = simple_getattr,
  467. };
  468. static int dlmfs_get_sb(struct file_system_type *fs_type,
  469. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  470. {
  471. return get_sb_nodev(fs_type, flags, data, dlmfs_fill_super, mnt);
  472. }
  473. static struct file_system_type dlmfs_fs_type = {
  474. .owner = THIS_MODULE,
  475. .name = "ocfs2_dlmfs",
  476. .get_sb = dlmfs_get_sb,
  477. .kill_sb = kill_litter_super,
  478. };
  479. static int __init init_dlmfs_fs(void)
  480. {
  481. int status;
  482. int cleanup_inode = 0, cleanup_worker = 0;
  483. dlmfs_print_version();
  484. dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
  485. sizeof(struct dlmfs_inode_private),
  486. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  487. SLAB_MEM_SPREAD),
  488. dlmfs_init_once, NULL);
  489. if (!dlmfs_inode_cache)
  490. return -ENOMEM;
  491. cleanup_inode = 1;
  492. user_dlm_worker = create_singlethread_workqueue("user_dlm");
  493. if (!user_dlm_worker) {
  494. status = -ENOMEM;
  495. goto bail;
  496. }
  497. cleanup_worker = 1;
  498. status = register_filesystem(&dlmfs_fs_type);
  499. bail:
  500. if (status) {
  501. if (cleanup_inode)
  502. kmem_cache_destroy(dlmfs_inode_cache);
  503. if (cleanup_worker)
  504. destroy_workqueue(user_dlm_worker);
  505. } else
  506. printk("OCFS2 User DLM kernel interface loaded\n");
  507. return status;
  508. }
  509. static void __exit exit_dlmfs_fs(void)
  510. {
  511. unregister_filesystem(&dlmfs_fs_type);
  512. flush_workqueue(user_dlm_worker);
  513. destroy_workqueue(user_dlm_worker);
  514. kmem_cache_destroy(dlmfs_inode_cache);
  515. }
  516. MODULE_AUTHOR("Oracle");
  517. MODULE_LICENSE("GPL");
  518. module_init(init_dlmfs_fs)
  519. module_exit(exit_dlmfs_fs)