inode.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * inode.c - basic inode and dentry operations.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. *
  21. * Based on sysfs:
  22. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  23. *
  24. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  25. *
  26. * Please see Documentation/filesystems/configfs.txt for more information.
  27. */
  28. #undef DEBUG
  29. #include <linux/pagemap.h>
  30. #include <linux/namei.h>
  31. #include <linux/backing-dev.h>
  32. #include <linux/configfs.h>
  33. #include "configfs_internal.h"
  34. extern struct super_block * configfs_sb;
  35. static struct address_space_operations configfs_aops = {
  36. .readpage = simple_readpage,
  37. .prepare_write = simple_prepare_write,
  38. .commit_write = simple_commit_write
  39. };
  40. static struct backing_dev_info configfs_backing_dev_info = {
  41. .ra_pages = 0, /* No readahead */
  42. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  43. };
  44. struct inode * configfs_new_inode(mode_t mode)
  45. {
  46. struct inode * inode = new_inode(configfs_sb);
  47. if (inode) {
  48. inode->i_mode = mode;
  49. inode->i_uid = 0;
  50. inode->i_gid = 0;
  51. inode->i_blksize = PAGE_CACHE_SIZE;
  52. inode->i_blocks = 0;
  53. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  54. inode->i_mapping->a_ops = &configfs_aops;
  55. inode->i_mapping->backing_dev_info = &configfs_backing_dev_info;
  56. }
  57. return inode;
  58. }
  59. int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
  60. {
  61. int error = 0;
  62. struct inode * inode = NULL;
  63. if (dentry) {
  64. if (!dentry->d_inode) {
  65. if ((inode = configfs_new_inode(mode))) {
  66. if (dentry->d_parent && dentry->d_parent->d_inode) {
  67. struct inode *p_inode = dentry->d_parent->d_inode;
  68. p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
  69. }
  70. goto Proceed;
  71. }
  72. else
  73. error = -ENOMEM;
  74. } else
  75. error = -EEXIST;
  76. } else
  77. error = -ENOENT;
  78. goto Done;
  79. Proceed:
  80. if (init)
  81. error = init(inode);
  82. if (!error) {
  83. d_instantiate(dentry, inode);
  84. if (S_ISDIR(mode) || S_ISLNK(mode))
  85. dget(dentry); /* pin link and directory dentries in core */
  86. } else
  87. iput(inode);
  88. Done:
  89. return error;
  90. }
  91. /*
  92. * Get the name for corresponding element represented by the given configfs_dirent
  93. */
  94. const unsigned char * configfs_get_name(struct configfs_dirent *sd)
  95. {
  96. struct attribute * attr;
  97. if (!sd || !sd->s_element)
  98. BUG();
  99. /* These always have a dentry, so use that */
  100. if (sd->s_type & (CONFIGFS_DIR | CONFIGFS_ITEM_LINK))
  101. return sd->s_dentry->d_name.name;
  102. if (sd->s_type & CONFIGFS_ITEM_ATTR) {
  103. attr = sd->s_element;
  104. return attr->name;
  105. }
  106. return NULL;
  107. }
  108. /*
  109. * Unhashes the dentry corresponding to given configfs_dirent
  110. * Called with parent inode's i_mutex held.
  111. */
  112. void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent)
  113. {
  114. struct dentry * dentry = sd->s_dentry;
  115. if (dentry) {
  116. spin_lock(&dcache_lock);
  117. if (!(d_unhashed(dentry) && dentry->d_inode)) {
  118. dget_locked(dentry);
  119. __d_drop(dentry);
  120. spin_unlock(&dcache_lock);
  121. simple_unlink(parent->d_inode, dentry);
  122. } else
  123. spin_unlock(&dcache_lock);
  124. }
  125. }
  126. void configfs_hash_and_remove(struct dentry * dir, const char * name)
  127. {
  128. struct configfs_dirent * sd;
  129. struct configfs_dirent * parent_sd = dir->d_fsdata;
  130. mutex_lock(&dir->d_inode->i_mutex);
  131. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  132. if (!sd->s_element)
  133. continue;
  134. if (!strcmp(configfs_get_name(sd), name)) {
  135. list_del_init(&sd->s_sibling);
  136. configfs_drop_dentry(sd, dir);
  137. configfs_put(sd);
  138. break;
  139. }
  140. }
  141. mutex_unlock(&dir->d_inode->i_mutex);
  142. }