dlmfs.c 15 KB

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