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.

344 lines
9.4 KiB

configfs: file.c fix possible recursive locking configfs_register_subsystem() with default_groups triggers recursive locking. it seems that mutex_lock_nested is needed. ============================================= [ INFO: possible recursive locking detected ] 2.6.24-rc6 #145 --------------------------------------------- swapper/1 is trying to acquire lock: (&sb->s_type->i_mutex_key#3){--..}, at: [<c40c9a9e>] configfs_add_file+0x2e/0x70 but task is already holding lock: (&sb->s_type->i_mutex_key#3){--..}, at: [<c40ca985>] configfs_register_subsystem+0x55/0x130 other info that might help us debug this: 1 lock held by swapper/1: #0: (&sb->s_type->i_mutex_key#3){--..}, at: [<c40ca985>] configfs_register_subsystem+0x55/0x130 stack backtrace: Pid: 1, comm: swapper Not tainted 2.6.24-rc6 #145 [<c40053ba>] show_trace_log_lvl+0x1a/0x30 [<c4005e82>] show_trace+0x12/0x20 [<c400687e>] dump_stack+0x6e/0x80 [<c404ec72>] __lock_acquire+0xe62/0x1120 [<c404efb2>] lock_acquire+0x82/0xa0 [<c43fda88>] mutex_lock_nested+0x98/0x2e0 [<c40c9a9e>] configfs_add_file+0x2e/0x70 [<c40c9b0c>] configfs_create_file+0x2c/0x40 [<c40ca639>] configfs_attach_item+0x139/0x220 [<c40ca734>] configfs_attach_group+0x14/0x140 [<c40ca7e9>] configfs_attach_group+0xc9/0x140 [<c40ca9f6>] configfs_register_subsystem+0xc6/0x130 [<c45c8186>] init_netconsole+0x2b6/0x300 [<c45a75f2>] kernel_init+0x142/0x320 [<c4004fb3>] kernel_thread_helper+0x7/0x14 ======================= Signed-off-by: Joonwoo Park <joonwpark81@gmail.com> Signed-off-by: Joel Becker <joel.becker@oracle.com> Signed-off-by: Mark Fasheh <mark.fasheh@oracle.com>
14 years ago
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c - operations for regular (text) files.
  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. #include <linux/fs.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/configfs.h>
  32. #include "configfs_internal.h"
  33. /*
  34. * A simple attribute can only be 4096 characters. Why 4k? Because the
  35. * original code limited it to PAGE_SIZE. That's a bad idea, though,
  36. * because an attribute of 16k on ia64 won't work on x86. So we limit to
  37. * 4k, our minimum common page size.
  38. */
  39. #define SIMPLE_ATTR_SIZE 4096
  40. struct configfs_buffer {
  41. size_t count;
  42. loff_t pos;
  43. char * page;
  44. struct configfs_item_operations * ops;
  45. struct mutex mutex;
  46. int needs_read_fill;
  47. };
  48. /**
  49. * fill_read_buffer - allocate and fill buffer from item.
  50. * @dentry: dentry pointer.
  51. * @buffer: data buffer for file.
  52. *
  53. * Allocate @buffer->page, if it hasn't been already, then call the
  54. * config_item's show() method to fill the buffer with this attribute's
  55. * data.
  56. * This is called only once, on the file's first read.
  57. */
  58. static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
  59. {
  60. struct configfs_attribute * attr = to_attr(dentry);
  61. struct config_item * item = to_item(dentry->d_parent);
  62. struct configfs_item_operations * ops = buffer->ops;
  63. int ret = 0;
  64. ssize_t count;
  65. if (!buffer->page)
  66. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  67. if (!buffer->page)
  68. return -ENOMEM;
  69. count = ops->show_attribute(item,attr,buffer->page);
  70. buffer->needs_read_fill = 0;
  71. BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
  72. if (count >= 0)
  73. buffer->count = count;
  74. else
  75. ret = count;
  76. return ret;
  77. }
  78. /**
  79. * configfs_read_file - read an attribute.
  80. * @file: file pointer.
  81. * @buf: buffer to fill.
  82. * @count: number of bytes to read.
  83. * @ppos: starting offset in file.
  84. *
  85. * Userspace wants to read an attribute file. The attribute descriptor
  86. * is in the file's ->d_fsdata. The target item is in the directory's
  87. * ->d_fsdata.
  88. *
  89. * We call fill_read_buffer() to allocate and fill the buffer from the
  90. * item's show() method exactly once (if the read is happening from
  91. * the beginning of the file). That should fill the entire buffer with
  92. * all the data the item has to offer for that attribute.
  93. * We then call flush_read_buffer() to copy the buffer to userspace
  94. * in the increments specified.
  95. */
  96. static ssize_t
  97. configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  98. {
  99. struct configfs_buffer * buffer = file->private_data;
  100. ssize_t retval = 0;
  101. mutex_lock(&buffer->mutex);
  102. if (buffer->needs_read_fill) {
  103. if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
  104. goto out;
  105. }
  106. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  107. __func__, count, *ppos, buffer->page);
  108. retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
  109. buffer->count);
  110. out:
  111. mutex_unlock(&buffer->mutex);
  112. return retval;
  113. }
  114. /**
  115. * fill_write_buffer - copy buffer from userspace.
  116. * @buffer: data buffer for file.
  117. * @buf: data from user.
  118. * @count: number of bytes in @userbuf.
  119. *
  120. * Allocate @buffer->page if it hasn't been already, then
  121. * copy the user-supplied buffer into it.
  122. */
  123. static int
  124. fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
  125. {
  126. int error;
  127. if (!buffer->page)
  128. buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
  129. if (!buffer->page)
  130. return -ENOMEM;
  131. if (count >= SIMPLE_ATTR_SIZE)
  132. count = SIMPLE_ATTR_SIZE - 1;
  133. error = copy_from_user(buffer->page,buf,count);
  134. buffer->needs_read_fill = 1;
  135. /* if buf is assumed to contain a string, terminate it by \0,
  136. * so e.g. sscanf() can scan the string easily */
  137. buffer->page[count] = 0;
  138. return error ? -EFAULT : count;
  139. }
  140. /**
  141. * flush_write_buffer - push buffer to config_item.
  142. * @dentry: dentry to the attribute
  143. * @buffer: data buffer for file.
  144. * @count: number of bytes
  145. *
  146. * Get the correct pointers for the config_item and the attribute we're
  147. * dealing with, then call the store() method for the attribute,
  148. * passing the buffer that we acquired in fill_write_buffer().
  149. */
  150. static int
  151. flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
  152. {
  153. struct configfs_attribute * attr = to_attr(dentry);
  154. struct config_item * item = to_item(dentry->d_parent);
  155. struct configfs_item_operations * ops = buffer->ops;
  156. return ops->store_attribute(item,attr,buffer->page,count);
  157. }
  158. /**
  159. * configfs_write_file - write an attribute.
  160. * @file: file pointer
  161. * @buf: data to write
  162. * @count: number of bytes
  163. * @ppos: starting offset
  164. *
  165. * Similar to configfs_read_file(), though working in the opposite direction.
  166. * We allocate and fill the data from the user in fill_write_buffer(),
  167. * then push it to the config_item in flush_write_buffer().
  168. * There is no easy way for us to know if userspace is only doing a partial
  169. * write, so we don't support them. We expect the entire buffer to come
  170. * on the first write.
  171. * Hint: if you're writing a value, first read the file, modify only the
  172. * the value you're changing, then write entire buffer back.
  173. */
  174. static ssize_t
  175. configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  176. {
  177. struct configfs_buffer * buffer = file->private_data;
  178. ssize_t len;
  179. mutex_lock(&buffer->mutex);
  180. len = fill_write_buffer(buffer, buf, count);
  181. if (len > 0)
  182. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  183. if (len > 0)
  184. *ppos += len;
  185. mutex_unlock(&buffer->mutex);
  186. return len;
  187. }
  188. static int check_perm(struct inode * inode, struct file * file)
  189. {
  190. struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
  191. struct configfs_attribute * attr = to_attr(file->f_path.dentry);
  192. struct configfs_buffer * buffer;
  193. struct configfs_item_operations * ops = NULL;
  194. int error = 0;
  195. if (!item || !attr)
  196. goto Einval;
  197. /* Grab the module reference for this attribute if we have one */
  198. if (!try_module_get(attr->ca_owner)) {
  199. error = -ENODEV;
  200. goto Done;
  201. }
  202. if (item->ci_type)
  203. ops = item->ci_type->ct_item_ops;
  204. else
  205. goto Eaccess;
  206. /* File needs write support.
  207. * The inode's perms must say it's ok,
  208. * and we must have a store method.
  209. */
  210. if (file->f_mode & FMODE_WRITE) {
  211. if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute)
  212. goto Eaccess;
  213. }
  214. /* File needs read support.
  215. * The inode's perms must say it's ok, and we there
  216. * must be a show method for it.
  217. */
  218. if (file->f_mode & FMODE_READ) {
  219. if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute)
  220. goto Eaccess;
  221. }
  222. /* No error? Great, allocate a buffer for the file, and store it
  223. * it in file->private_data for easy access.
  224. */
  225. buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
  226. if (!buffer) {
  227. error = -ENOMEM;
  228. goto Enomem;
  229. }
  230. mutex_init(&buffer->mutex);
  231. buffer->needs_read_fill = 1;
  232. buffer->ops = ops;
  233. file->private_data = buffer;
  234. goto Done;
  235. Einval:
  236. error = -EINVAL;
  237. goto Done;
  238. Eaccess:
  239. error = -EACCES;
  240. Enomem:
  241. module_put(attr->ca_owner);
  242. Done:
  243. if (error && item)
  244. config_item_put(item);
  245. return error;
  246. }
  247. static int configfs_open_file(struct inode * inode, struct file * filp)
  248. {
  249. return check_perm(inode,filp);
  250. }
  251. static int configfs_release(struct inode * inode, struct file * filp)
  252. {
  253. struct config_item * item = to_item(filp->f_path.dentry->d_parent);
  254. struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
  255. struct module * owner = attr->ca_owner;
  256. struct configfs_buffer * buffer = filp->private_data;
  257. if (item)
  258. config_item_put(item);
  259. /* After this point, attr should not be accessed. */
  260. module_put(owner);
  261. if (buffer) {
  262. if (buffer->page)
  263. free_page((unsigned long)buffer->page);
  264. mutex_destroy(&buffer->mutex);
  265. kfree(buffer);
  266. }
  267. return 0;
  268. }
  269. const struct file_operations configfs_file_operations = {
  270. .read = configfs_read_file,
  271. .write = configfs_write_file,
  272. .llseek = generic_file_llseek,
  273. .open = configfs_open_file,
  274. .release = configfs_release,
  275. };
  276. int configfs_add_file(struct dentry * dir, const struct configfs_attribute * attr, int type)
  277. {
  278. struct configfs_dirent * parent_sd = dir->d_fsdata;
  279. umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
  280. int error = 0;
  281. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_NORMAL);
  282. error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
  283. mutex_unlock(&dir->d_inode->i_mutex);
  284. return error;
  285. }
  286. /**
  287. * configfs_create_file - create an attribute file for an item.
  288. * @item: item we're creating for.
  289. * @attr: atrribute descriptor.
  290. */
  291. int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
  292. {
  293. BUG_ON(!item || !item->ci_dentry || !attr);
  294. return configfs_add_file(item->ci_dentry, attr,
  295. CONFIGFS_ITEM_ATTR);
  296. }