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.

765 lines
16 KiB

llseek: automatically add .llseek fop All file_operations should get a .llseek operation so we can make nonseekable_open the default for future file operations without a .llseek pointer. The three cases that we can automatically detect are no_llseek, seq_lseek and default_llseek. For cases where we can we can automatically prove that the file offset is always ignored, we use noop_llseek, which maintains the current behavior of not returning an error from a seek. New drivers should normally not use noop_llseek but instead use no_llseek and call nonseekable_open at open time. Existing drivers can be converted to do the same when the maintainer knows for certain that no user code relies on calling seek on the device file. The generated code is often incorrectly indented and right now contains comments that clarify for each added line why a specific variant was chosen. In the version that gets submitted upstream, the comments will be gone and I will manually fix the indentation, because there does not seem to be a way to do that using coccinelle. Some amount of new code is currently sitting in linux-next that should get the same modifications, which I will do at the end of the merge window. Many thanks to Julia Lawall for helping me learn to write a semantic patch that does all this. ===== begin semantic patch ===== // This adds an llseek= method to all file operations, // as a preparation for making no_llseek the default. // // The rules are // - use no_llseek explicitly if we do nonseekable_open // - use seq_lseek for sequential files // - use default_llseek if we know we access f_pos // - use noop_llseek if we know we don't access f_pos, // but we still want to allow users to call lseek // @ open1 exists @ identifier nested_open; @@ nested_open(...) { <+... nonseekable_open(...) ...+> } @ open exists@ identifier open_f; identifier i, f; identifier open1.nested_open; @@ int open_f(struct inode *i, struct file *f) { <+... ( nonseekable_open(...) | nested_open(...) ) ...+> } @ read disable optional_qualifier exists @ identifier read_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; expression E; identifier func; @@ ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off) { <+... ( *off = E | *off += E | func(..., off, ...) | E = *off ) ...+> } @ read_no_fpos disable optional_qualifier exists @ identifier read_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; @@ ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off) { ... when != off } @ write @ identifier write_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; expression E; identifier func; @@ ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off) { <+... ( *off = E | *off += E | func(..., off, ...) | E = *off ) ...+> } @ write_no_fpos @ identifier write_f; identifier f, p, s, off; type ssize_t, size_t, loff_t; @@ ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off) { ... when != off } @ fops0 @ identifier fops; @@ struct file_operations fops = { ... }; @ has_llseek depends on fops0 @ identifier fops0.fops; identifier llseek_f; @@ struct file_operations fops = { ... .llseek = llseek_f, ... }; @ has_read depends on fops0 @ identifier fops0.fops; identifier read_f; @@ struct file_operations fops = { ... .read = read_f, ... }; @ has_write depends on fops0 @ identifier fops0.fops; identifier write_f; @@ struct file_operations fops = { ... .write = write_f, ... }; @ has_open depends on fops0 @ identifier fops0.fops; identifier open_f; @@ struct file_operations fops = { ... .open = open_f, ... }; // use no_llseek if we call nonseekable_open //////////////////////////////////////////// @ nonseekable1 depends on !has_llseek && has_open @ identifier fops0.fops; identifier nso ~= "nonseekable_open"; @@ struct file_operations fops = { ... .open = nso, ... +.llseek = no_llseek, /* nonseekable */ }; @ nonseekable2 depends on !has_llseek @ identifier fops0.fops; identifier open.open_f; @@ struct file_operations fops = { ... .open = open_f, ... +.llseek = no_llseek, /* open uses nonseekable */ }; // use seq_lseek for sequential files ///////////////////////////////////// @ seq depends on !has_llseek @ identifier fops0.fops; identifier sr ~= "seq_read"; @@ struct file_operations fops = { ... .read = sr, ... +.llseek = seq_lseek, /* we have seq_read */ }; // use default_llseek if there is a readdir /////////////////////////////////////////// @ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier readdir_e; @@ // any other fop is used that changes pos struct file_operations fops = { ... .readdir = readdir_e, ... +.llseek = default_llseek, /* readdir is present */ }; // use default_llseek if at least one of read/write touches f_pos ///////////////////////////////////////////////////////////////// @ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read.read_f; @@ // read fops use offset struct file_operations fops = { ... .read = read_f, ... +.llseek = default_llseek, /* read accesses f_pos */ }; @ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier write.write_f; @@ // write fops use offset struct file_operations fops = { ... .write = write_f, ... + .llseek = default_llseek, /* write accesses f_pos */ }; // Use noop_llseek if neither read nor write accesses f_pos /////////////////////////////////////////////////////////// @ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read_no_fpos.read_f; identifier write_no_fpos.write_f; @@ // write fops use offset struct file_operations fops = { ... .write = write_f, .read = read_f, ... +.llseek = noop_llseek, /* read and write both use no f_pos */ }; @ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier write_no_fpos.write_f; @@ struct file_operations fops = { ... .write = write_f, ... +.llseek = noop_llseek, /* write uses no f_pos */ }; @ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; identifier read_no_fpos.read_f; @@ struct file_operations fops = { ... .read = read_f, ... +.llseek = noop_llseek, /* read uses no f_pos */ }; @ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @ identifier fops0.fops; @@ struct file_operations fops = { ... +.llseek = noop_llseek, /* no read or write fn */ }; ===== End semantic patch ===== Signed-off-by: Arnd Bergmann <arnd@arndb.de> Cc: Julia Lawall <julia@diku.dk> Cc: Christoph Hellwig <hch@infradead.org>
11 years ago
[PATCH] VFS: Permit filesystem to override root dentry on mount Extend the get_sb() filesystem operation to take an extra argument that permits the VFS to pass in the target vfsmount that defines the mountpoint. The filesystem is then required to manually set the superblock and root dentry pointers. For most filesystems, this should be done with simple_set_mnt() which will set the superblock pointer and then set the root dentry to the superblock's s_root (as per the old default behaviour). The get_sb() op now returns an integer as there's now no need to return the superblock pointer. This patch permits a superblock to be implicitly shared amongst several mount points, such as can be done with NFS to avoid potential inode aliasing. In such a case, simple_set_mnt() would not be called, and instead the mnt_root and mnt_sb would be set directly. The patch also makes the following changes: (*) the get_sb_*() convenience functions in the core kernel now take a vfsmount pointer argument and return an integer, so most filesystems have to change very little. (*) If one of the convenience function is not used, then get_sb() should normally call simple_set_mnt() to instantiate the vfsmount. This will always return 0, and so can be tail-called from get_sb(). (*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the dcache upon superblock destruction rather than shrink_dcache_anon(). This is required because the superblock may now have multiple trees that aren't actually bound to s_root, but that still need to be cleaned up. The currently called functions assume that the whole tree is rooted at s_root, and that anonymous dentries are not the roots of trees which results in dentries being left unculled. However, with the way NFS superblock sharing are currently set to be implemented, these assumptions are violated: the root of the filesystem is simply a dummy dentry and inode (the real inode for '/' may well be inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries with child trees. [*] Anonymous until discovered from another tree. (*) The documentation has been adjusted, including the additional bit of changing ext2_* into foo_* in the documentation. [akpm@osdl.org: convert ipath_fs, do other stuff] Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Cc: Nathan Scott <nathans@sgi.com> Cc: Roland Dreier <rolandd@cisco.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
16 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/ctype.h>
  6. #include <linux/dcache.h>
  7. #include <linux/file.h>
  8. #include <linux/fs.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/mount.h>
  14. #include <linux/slab.h>
  15. #include <linux/statfs.h>
  16. #include <linux/types.h>
  17. #include <linux/pid_namespace.h>
  18. #include <linux/namei.h>
  19. #include <asm/uaccess.h>
  20. #include <os.h>
  21. static struct inode *get_inode(struct super_block *, struct dentry *);
  22. struct hppfs_data {
  23. struct list_head list;
  24. char contents[PAGE_SIZE - sizeof(struct list_head)];
  25. };
  26. struct hppfs_private {
  27. struct file *proc_file;
  28. int host_fd;
  29. loff_t len;
  30. struct hppfs_data *contents;
  31. };
  32. struct hppfs_inode_info {
  33. struct dentry *proc_dentry;
  34. struct inode vfs_inode;
  35. };
  36. static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
  37. {
  38. return container_of(inode, struct hppfs_inode_info, vfs_inode);
  39. }
  40. #define HPPFS_SUPER_MAGIC 0xb00000ee
  41. static const struct super_operations hppfs_sbops;
  42. static int is_pid(struct dentry *dentry)
  43. {
  44. struct super_block *sb;
  45. int i;
  46. sb = dentry->d_sb;
  47. if (dentry->d_parent != sb->s_root)
  48. return 0;
  49. for (i = 0; i < dentry->d_name.len; i++) {
  50. if (!isdigit(dentry->d_name.name[i]))
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. static char *dentry_name(struct dentry *dentry, int extra)
  56. {
  57. struct dentry *parent;
  58. char *root, *name;
  59. const char *seg_name;
  60. int len, seg_len;
  61. len = 0;
  62. parent = dentry;
  63. while (parent->d_parent != parent) {
  64. if (is_pid(parent))
  65. len += strlen("pid") + 1;
  66. else len += parent->d_name.len + 1;
  67. parent = parent->d_parent;
  68. }
  69. root = "proc";
  70. len += strlen(root);
  71. name = kmalloc(len + extra + 1, GFP_KERNEL);
  72. if (name == NULL)
  73. return NULL;
  74. name[len] = '\0';
  75. parent = dentry;
  76. while (parent->d_parent != parent) {
  77. if (is_pid(parent)) {
  78. seg_name = "pid";
  79. seg_len = strlen("pid");
  80. }
  81. else {
  82. seg_name = parent->d_name.name;
  83. seg_len = parent->d_name.len;
  84. }
  85. len -= seg_len + 1;
  86. name[len] = '/';
  87. strncpy(&name[len + 1], seg_name, seg_len);
  88. parent = parent->d_parent;
  89. }
  90. strncpy(name, root, strlen(root));
  91. return name;
  92. }
  93. static int file_removed(struct dentry *dentry, const char *file)
  94. {
  95. char *host_file;
  96. int extra, fd;
  97. extra = 0;
  98. if (file != NULL)
  99. extra += strlen(file) + 1;
  100. host_file = dentry_name(dentry, extra + strlen("/remove"));
  101. if (host_file == NULL) {
  102. printk(KERN_ERR "file_removed : allocation failed\n");
  103. return -ENOMEM;
  104. }
  105. if (file != NULL) {
  106. strcat(host_file, "/");
  107. strcat(host_file, file);
  108. }
  109. strcat(host_file, "/remove");
  110. fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
  111. kfree(host_file);
  112. if (fd > 0) {
  113. os_close_file(fd);
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
  119. unsigned int flags)
  120. {
  121. struct dentry *proc_dentry, *parent;
  122. struct qstr *name = &dentry->d_name;
  123. struct inode *inode;
  124. int err, deleted;
  125. deleted = file_removed(dentry, NULL);
  126. if (deleted < 0)
  127. return ERR_PTR(deleted);
  128. else if (deleted)
  129. return ERR_PTR(-ENOENT);
  130. parent = HPPFS_I(ino)->proc_dentry;
  131. mutex_lock(&parent->d_inode->i_mutex);
  132. proc_dentry = lookup_one_len(name->name, parent, name->len);
  133. mutex_unlock(&parent->d_inode->i_mutex);
  134. if (IS_ERR(proc_dentry))
  135. return proc_dentry;
  136. err = -ENOMEM;
  137. inode = get_inode(ino->i_sb, proc_dentry);
  138. if (!inode)
  139. goto out;
  140. d_add(dentry, inode);
  141. return NULL;
  142. out:
  143. return ERR_PTR(err);
  144. }
  145. static const struct inode_operations hppfs_file_iops = {
  146. };
  147. static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
  148. loff_t *ppos, int is_user)
  149. {
  150. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  151. ssize_t n;
  152. read = file_inode(file)->i_fop->read;
  153. if (!is_user)
  154. set_fs(KERNEL_DS);
  155. n = (*read)(file, buf, count, &file->f_pos);
  156. if (!is_user)
  157. set_fs(USER_DS);
  158. if (ppos)
  159. *ppos = file->f_pos;
  160. return n;
  161. }
  162. static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
  163. {
  164. ssize_t n;
  165. int cur, err;
  166. char *new_buf;
  167. n = -ENOMEM;
  168. new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  169. if (new_buf == NULL) {
  170. printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
  171. goto out;
  172. }
  173. n = 0;
  174. while (count > 0) {
  175. cur = min_t(ssize_t, count, PAGE_SIZE);
  176. err = os_read_file(fd, new_buf, cur);
  177. if (err < 0) {
  178. printk(KERN_ERR "hppfs_read : read failed, "
  179. "errno = %d\n", err);
  180. n = err;
  181. goto out_free;
  182. } else if (err == 0)
  183. break;
  184. if (copy_to_user(buf, new_buf, err)) {
  185. n = -EFAULT;
  186. goto out_free;
  187. }
  188. n += err;
  189. count -= err;
  190. }
  191. out_free:
  192. kfree(new_buf);
  193. out:
  194. return n;
  195. }
  196. static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
  197. loff_t *ppos)
  198. {
  199. struct hppfs_private *hppfs = file->private_data;
  200. struct hppfs_data *data;
  201. loff_t off;
  202. int err;
  203. if (hppfs->contents != NULL) {
  204. int rem;
  205. if (*ppos >= hppfs->len)
  206. return 0;
  207. data = hppfs->contents;
  208. off = *ppos;
  209. while (off >= sizeof(data->contents)) {
  210. data = list_entry(data->list.next, struct hppfs_data,
  211. list);
  212. off -= sizeof(data->contents);
  213. }
  214. if (off + count > hppfs->len)
  215. count = hppfs->len - off;
  216. rem = copy_to_user(buf, &data->contents[off], count);
  217. *ppos += count - rem;
  218. if (rem > 0)
  219. return -EFAULT;
  220. } else if (hppfs->host_fd != -1) {
  221. err = os_seek_file(hppfs->host_fd, *ppos);
  222. if (err) {
  223. printk(KERN_ERR "hppfs_read : seek failed, "
  224. "errno = %d\n", err);
  225. return err;
  226. }
  227. err = hppfs_read_file(hppfs->host_fd, buf, count);
  228. if (err < 0) {
  229. printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
  230. return err;
  231. }
  232. count = err;
  233. if (count > 0)
  234. *ppos += count;
  235. }
  236. else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
  237. return count;
  238. }
  239. static ssize_t hppfs_write(struct file *file, const char __user *buf,
  240. size_t len, loff_t *ppos)
  241. {
  242. struct hppfs_private *data = file->private_data;
  243. struct file *proc_file = data->proc_file;
  244. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  245. write = file_inode(proc_file)->i_fop->write;
  246. return (*write)(proc_file, buf, len, ppos);
  247. }
  248. static int open_host_sock(char *host_file, int *filter_out)
  249. {
  250. char *end;
  251. int fd;
  252. end = &host_file[strlen(host_file)];
  253. strcpy(end, "/rw");
  254. *filter_out = 1;
  255. fd = os_connect_socket(host_file);
  256. if (fd > 0)
  257. return fd;
  258. strcpy(end, "/r");
  259. *filter_out = 0;
  260. fd = os_connect_socket(host_file);
  261. return fd;
  262. }
  263. static void free_contents(struct hppfs_data *head)
  264. {
  265. struct hppfs_data *data;
  266. struct list_head *ele, *next;
  267. if (head == NULL)
  268. return;
  269. list_for_each_safe(ele, next, &head->list) {
  270. data = list_entry(ele, struct hppfs_data, list);
  271. kfree(data);
  272. }
  273. kfree(head);
  274. }
  275. static struct hppfs_data *hppfs_get_data(int fd, int filter,
  276. struct file *proc_file,
  277. struct file *hppfs_file,
  278. loff_t *size_out)
  279. {
  280. struct hppfs_data *data, *new, *head;
  281. int n, err;
  282. err = -ENOMEM;
  283. data = kmalloc(sizeof(*data), GFP_KERNEL);
  284. if (data == NULL) {
  285. printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
  286. goto failed;
  287. }
  288. INIT_LIST_HEAD(&data->list);
  289. head = data;
  290. *size_out = 0;
  291. if (filter) {
  292. while ((n = read_proc(proc_file, data->contents,
  293. sizeof(data->contents), NULL, 0)) > 0)
  294. os_write_file(fd, data->contents, n);
  295. err = os_shutdown_socket(fd, 0, 1);
  296. if (err) {
  297. printk(KERN_ERR "hppfs_get_data : failed to shut down "
  298. "socket\n");
  299. goto failed_free;
  300. }
  301. }
  302. while (1) {
  303. n = os_read_file(fd, data->contents, sizeof(data->contents));
  304. if (n < 0) {
  305. err = n;
  306. printk(KERN_ERR "hppfs_get_data : read failed, "
  307. "errno = %d\n", err);
  308. goto failed_free;
  309. } else if (n == 0)
  310. break;
  311. *size_out += n;
  312. if (n < sizeof(data->contents))
  313. break;
  314. new = kmalloc(sizeof(*data), GFP_KERNEL);
  315. if (new == 0) {
  316. printk(KERN_ERR "hppfs_get_data : data allocation "
  317. "failed\n");
  318. err = -ENOMEM;
  319. goto failed_free;
  320. }
  321. INIT_LIST_HEAD(&new->list);
  322. list_add(&new->list, &data->list);
  323. data = new;
  324. }
  325. return head;
  326. failed_free:
  327. free_contents(head);
  328. failed:
  329. return ERR_PTR(err);
  330. }
  331. static struct hppfs_private *hppfs_data(void)
  332. {
  333. struct hppfs_private *data;
  334. data = kmalloc(sizeof(*data), GFP_KERNEL);
  335. if (data == NULL)
  336. return data;
  337. *data = ((struct hppfs_private ) { .host_fd = -1,
  338. .len = -1,
  339. .contents = NULL } );
  340. return data;
  341. }
  342. static int file_mode(int fmode)
  343. {
  344. if (fmode == (FMODE_READ | FMODE_WRITE))
  345. return O_RDWR;
  346. if (fmode == FMODE_READ)
  347. return O_RDONLY;
  348. if (fmode == FMODE_WRITE)
  349. return O_WRONLY;
  350. return 0;
  351. }
  352. static int hppfs_open(struct inode *inode, struct file *file)
  353. {
  354. const struct cred *cred = file->f_cred;
  355. struct hppfs_private *data;
  356. struct path path;
  357. char *host_file;
  358. int err, fd, type, filter;
  359. err = -ENOMEM;
  360. data = hppfs_data();
  361. if (data == NULL)
  362. goto out;
  363. host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
  364. if (host_file == NULL)
  365. goto out_free2;
  366. path.mnt = inode->i_sb->s_fs_info;
  367. path.dentry = HPPFS_I(inode)->proc_dentry;
  368. /* XXX This isn't closed anywhere */
  369. data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
  370. err = PTR_ERR(data->proc_file);
  371. if (IS_ERR(data->proc_file))
  372. goto out_free1;
  373. type = os_file_type(host_file);
  374. if (type == OS_TYPE_FILE) {
  375. fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
  376. if (fd >= 0)
  377. data->host_fd = fd;
  378. else
  379. printk(KERN_ERR "hppfs_open : failed to open '%s', "
  380. "errno = %d\n", host_file, -fd);
  381. data->contents = NULL;
  382. } else if (type == OS_TYPE_DIR) {
  383. fd = open_host_sock(host_file, &filter);
  384. if (fd > 0) {
  385. data->contents = hppfs_get_data(fd, filter,
  386. data->proc_file,
  387. file, &data->len);
  388. if (!IS_ERR(data->contents))
  389. data->host_fd = fd;
  390. } else
  391. printk(KERN_ERR "hppfs_open : failed to open a socket "
  392. "in '%s', errno = %d\n", host_file, -fd);
  393. }
  394. kfree(host_file);
  395. file->private_data = data;
  396. return 0;
  397. out_free1:
  398. kfree(host_file);
  399. out_free2:
  400. free_contents(data->contents);
  401. kfree(data);
  402. out:
  403. return err;
  404. }
  405. static int hppfs_dir_open(struct inode *inode, struct file *file)
  406. {
  407. const struct cred *cred = file->f_cred;
  408. struct hppfs_private *data;
  409. struct path path;
  410. int err;
  411. err = -ENOMEM;
  412. data = hppfs_data();
  413. if (data == NULL)
  414. goto out;
  415. path.mnt = inode->i_sb->s_fs_info;
  416. path.dentry = HPPFS_I(inode)->proc_dentry;
  417. data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
  418. err = PTR_ERR(data->proc_file);
  419. if (IS_ERR(data->proc_file))
  420. goto out_free;
  421. file->private_data = data;
  422. return 0;
  423. out_free:
  424. kfree(data);
  425. out:
  426. return err;
  427. }
  428. static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
  429. {
  430. struct hppfs_private *data = file->private_data;
  431. struct file *proc_file = data->proc_file;
  432. loff_t (*llseek)(struct file *, loff_t, int);
  433. loff_t ret;
  434. llseek = file_inode(proc_file)->i_fop->llseek;
  435. if (llseek != NULL) {
  436. ret = (*llseek)(proc_file, off, where);
  437. if (ret < 0)
  438. return ret;
  439. }
  440. return default_llseek(file, off, where);
  441. }
  442. static const struct file_operations hppfs_file_fops = {
  443. .owner = NULL,
  444. .llseek = hppfs_llseek,
  445. .read = hppfs_read,
  446. .write = hppfs_write,
  447. .open = hppfs_open,
  448. };
  449. struct hppfs_dirent {
  450. void *vfs_dirent;
  451. filldir_t filldir;
  452. struct dentry *dentry;
  453. };
  454. static int hppfs_filldir(void *d, const char *name, int size,
  455. loff_t offset, u64 inode, unsigned int type)
  456. {
  457. struct hppfs_dirent *dirent = d;
  458. if (file_removed(dirent->dentry, name))
  459. return 0;
  460. return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
  461. inode, type);
  462. }
  463. static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
  464. {
  465. struct hppfs_private *data = file->private_data;
  466. struct file *proc_file = data->proc_file;
  467. int (*readdir)(struct file *, void *, filldir_t);
  468. struct hppfs_dirent dirent = ((struct hppfs_dirent)
  469. { .vfs_dirent = ent,
  470. .filldir = filldir,
  471. .dentry = file->f_path.dentry
  472. });
  473. int err;
  474. readdir = file_inode(proc_file)->i_fop->readdir;
  475. proc_file->f_pos = file->f_pos;
  476. err = (*readdir)(proc_file, &dirent, hppfs_filldir);
  477. file->f_pos = proc_file->f_pos;
  478. return err;
  479. }
  480. static int hppfs_fsync(struct file *file, loff_t start, loff_t end,
  481. int datasync)
  482. {
  483. return filemap_write_and_wait_range(file->f_mapping, start, end);
  484. }
  485. static const struct file_operations hppfs_dir_fops = {
  486. .owner = NULL,
  487. .readdir = hppfs_readdir,
  488. .open = hppfs_dir_open,
  489. .fsync = hppfs_fsync,
  490. .llseek = default_llseek,
  491. };
  492. static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  493. {
  494. sf->f_blocks = 0;
  495. sf->f_bfree = 0;
  496. sf->f_bavail = 0;
  497. sf->f_files = 0;
  498. sf->f_ffree = 0;
  499. sf->f_type = HPPFS_SUPER_MAGIC;
  500. return 0;
  501. }
  502. static struct inode *hppfs_alloc_inode(struct super_block *sb)
  503. {
  504. struct hppfs_inode_info *hi;
  505. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  506. if (!hi)
  507. return NULL;
  508. hi->proc_dentry = NULL;
  509. inode_init_once(&hi->vfs_inode);
  510. return &hi->vfs_inode;
  511. }
  512. void hppfs_evict_inode(struct inode *ino)
  513. {
  514. clear_inode(ino);
  515. dput(HPPFS_I(ino)->proc_dentry);
  516. mntput(ino->i_sb->s_fs_info);
  517. }
  518. static void hppfs_i_callback(struct rcu_head *head)
  519. {
  520. struct inode *inode = container_of(head, struct inode, i_rcu);
  521. kfree(HPPFS_I(inode));
  522. }
  523. static void hppfs_destroy_inode(struct inode *inode)
  524. {
  525. call_rcu(&inode->i_rcu, hppfs_i_callback);
  526. }
  527. static const struct super_operations hppfs_sbops = {
  528. .alloc_inode = hppfs_alloc_inode,
  529. .destroy_inode = hppfs_destroy_inode,
  530. .evict_inode = hppfs_evict_inode,
  531. .statfs = hppfs_statfs,
  532. };
  533. static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
  534. int buflen)
  535. {
  536. struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
  537. return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
  538. buflen);
  539. }
  540. static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  541. {
  542. struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
  543. return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
  544. }
  545. static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
  546. void *cookie)
  547. {
  548. struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
  549. if (proc_dentry->d_inode->i_op->put_link)
  550. proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
  551. }
  552. static const struct inode_operations hppfs_dir_iops = {
  553. .lookup = hppfs_lookup,
  554. };
  555. static const struct inode_operations hppfs_link_iops = {
  556. .readlink = hppfs_readlink,
  557. .follow_link = hppfs_follow_link,
  558. .put_link = hppfs_put_link,
  559. };
  560. static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
  561. {
  562. struct inode *proc_ino = dentry->d_inode;
  563. struct inode *inode = new_inode(sb);
  564. if (!inode) {
  565. dput(dentry);
  566. return NULL;
  567. }
  568. if (S_ISDIR(dentry->d_inode->i_mode)) {
  569. inode->i_op = &hppfs_dir_iops;
  570. inode->i_fop = &hppfs_dir_fops;
  571. } else if (S_ISLNK(dentry->d_inode->i_mode)) {
  572. inode->i_op = &hppfs_link_iops;
  573. inode->i_fop = &hppfs_file_fops;
  574. } else {
  575. inode->i_op = &hppfs_file_iops;
  576. inode->i_fop = &hppfs_file_fops;
  577. }
  578. HPPFS_I(inode)->proc_dentry = dentry;
  579. inode->i_uid = proc_ino->i_uid;
  580. inode->i_gid = proc_ino->i_gid;
  581. inode->i_atime = proc_ino->i_atime;
  582. inode->i_mtime = proc_ino->i_mtime;
  583. inode->i_ctime = proc_ino->i_ctime;
  584. inode->i_ino = proc_ino->i_ino;
  585. inode->i_mode = proc_ino->i_mode;
  586. set_nlink(inode, proc_ino->i_nlink);
  587. inode->i_size = proc_ino->i_size;
  588. inode->i_blocks = proc_ino->i_blocks;
  589. return inode;
  590. }
  591. static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
  592. {
  593. struct inode *root_inode;
  594. struct vfsmount *proc_mnt;
  595. int err = -ENOENT;
  596. proc_mnt = mntget(task_active_pid_ns(current)->proc_mnt);
  597. if (IS_ERR(proc_mnt))
  598. goto out;
  599. sb->s_blocksize = 1024;
  600. sb->s_blocksize_bits = 10;
  601. sb->s_magic = HPPFS_SUPER_MAGIC;
  602. sb->s_op = &hppfs_sbops;
  603. sb->s_fs_info = proc_mnt;
  604. err = -ENOMEM;
  605. root_inode = get_inode(sb, dget(proc_mnt->mnt_root));
  606. sb->s_root = d_make_root(root_inode);
  607. if (!sb->s_root)
  608. goto out_mntput;
  609. return 0;
  610. out_mntput:
  611. mntput(proc_mnt);
  612. out:
  613. return(err);
  614. }
  615. static struct dentry *hppfs_read_super(struct file_system_type *type,
  616. int flags, const char *dev_name,
  617. void *data)
  618. {
  619. return mount_nodev(type, flags, data, hppfs_fill_super);
  620. }
  621. static struct file_system_type hppfs_type = {
  622. .owner = THIS_MODULE,
  623. .name = "hppfs",
  624. .mount = hppfs_read_super,
  625. .kill_sb = kill_anon_super,
  626. .fs_flags = 0,
  627. };
  628. MODULE_ALIAS_FS("hppfs");
  629. static int __init init_hppfs(void)
  630. {
  631. return register_filesystem(&hppfs_type);
  632. }
  633. static void __exit exit_hppfs(void)
  634. {
  635. unregister_filesystem(&hppfs_type);
  636. }
  637. module_init(init_hppfs)
  638. module_exit(exit_hppfs)
  639. MODULE_LICENSE("GPL");