hppfs_kern.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 <asm/uaccess.h>
  18. #include "os.h"
  19. static int init_inode(struct inode *inode, struct dentry *dentry,
  20. struct vfsmount *mnt);
  21. struct hppfs_data {
  22. struct list_head list;
  23. char contents[PAGE_SIZE - sizeof(struct list_head)];
  24. };
  25. struct hppfs_private {
  26. struct file *proc_file;
  27. int host_fd;
  28. loff_t len;
  29. struct hppfs_data *contents;
  30. };
  31. struct hppfs_inode_info {
  32. struct dentry *proc_dentry;
  33. struct vfsmount *proc_mnt;
  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 ((sb->s_op != &hppfs_sbops) || (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 void hppfs_read_inode(struct inode *ino)
  119. {
  120. struct inode *proc_ino;
  121. if (HPPFS_I(ino)->proc_dentry == NULL)
  122. return;
  123. proc_ino = HPPFS_I(ino)->proc_dentry->d_inode;
  124. ino->i_uid = proc_ino->i_uid;
  125. ino->i_gid = proc_ino->i_gid;
  126. ino->i_atime = proc_ino->i_atime;
  127. ino->i_mtime = proc_ino->i_mtime;
  128. ino->i_ctime = proc_ino->i_ctime;
  129. ino->i_ino = proc_ino->i_ino;
  130. ino->i_mode = proc_ino->i_mode;
  131. ino->i_nlink = proc_ino->i_nlink;
  132. ino->i_size = proc_ino->i_size;
  133. ino->i_blocks = proc_ino->i_blocks;
  134. }
  135. static struct inode *hppfs_iget(struct super_block *sb)
  136. {
  137. struct inode *inode;
  138. inode = iget_locked(sb, 0);
  139. if (!inode)
  140. return ERR_PTR(-ENOMEM);
  141. if (inode->i_state & I_NEW) {
  142. hppfs_read_inode(inode);
  143. unlock_new_inode(inode);
  144. }
  145. return inode;
  146. }
  147. static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
  148. struct nameidata *nd)
  149. {
  150. struct dentry *proc_dentry, *new, *parent;
  151. struct inode *inode;
  152. int err, deleted;
  153. deleted = file_removed(dentry, NULL);
  154. if (deleted < 0)
  155. return ERR_PTR(deleted);
  156. else if (deleted)
  157. return ERR_PTR(-ENOENT);
  158. err = -ENOMEM;
  159. parent = HPPFS_I(ino)->proc_dentry;
  160. mutex_lock(&parent->d_inode->i_mutex);
  161. proc_dentry = d_lookup(parent, &dentry->d_name);
  162. if (proc_dentry == NULL) {
  163. proc_dentry = d_alloc(parent, &dentry->d_name);
  164. if (proc_dentry == NULL) {
  165. mutex_unlock(&parent->d_inode->i_mutex);
  166. goto out;
  167. }
  168. new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
  169. proc_dentry, NULL);
  170. if (new) {
  171. dput(proc_dentry);
  172. proc_dentry = new;
  173. }
  174. }
  175. mutex_unlock(&parent->d_inode->i_mutex);
  176. if (IS_ERR(proc_dentry))
  177. return proc_dentry;
  178. inode = hppfs_iget(ino->i_sb);
  179. if (IS_ERR(inode)) {
  180. err = PTR_ERR(inode);
  181. goto out_dput;
  182. }
  183. err = init_inode(inode, proc_dentry, HPPFS_I(ino)->proc_mnt);
  184. if (err)
  185. goto out_put;
  186. hppfs_read_inode(inode);
  187. d_add(dentry, inode);
  188. return NULL;
  189. out_put:
  190. iput(inode);
  191. out_dput:
  192. dput(proc_dentry);
  193. out:
  194. return ERR_PTR(err);
  195. }
  196. static const struct inode_operations hppfs_file_iops = {
  197. };
  198. static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
  199. loff_t *ppos, int is_user)
  200. {
  201. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  202. ssize_t n;
  203. read = file->f_path.dentry->d_inode->i_fop->read;
  204. if (!is_user)
  205. set_fs(KERNEL_DS);
  206. n = (*read)(file, buf, count, &file->f_pos);
  207. if (!is_user)
  208. set_fs(USER_DS);
  209. if (ppos)
  210. *ppos = file->f_pos;
  211. return n;
  212. }
  213. static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
  214. {
  215. ssize_t n;
  216. int cur, err;
  217. char *new_buf;
  218. n = -ENOMEM;
  219. new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  220. if (new_buf == NULL) {
  221. printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
  222. goto out;
  223. }
  224. n = 0;
  225. while (count > 0) {
  226. cur = min_t(ssize_t, count, PAGE_SIZE);
  227. err = os_read_file(fd, new_buf, cur);
  228. if (err < 0) {
  229. printk(KERN_ERR "hppfs_read : read failed, "
  230. "errno = %d\n", err);
  231. n = err;
  232. goto out_free;
  233. } else if (err == 0)
  234. break;
  235. if (copy_to_user(buf, new_buf, err)) {
  236. n = -EFAULT;
  237. goto out_free;
  238. }
  239. n += err;
  240. count -= err;
  241. }
  242. out_free:
  243. kfree(new_buf);
  244. out:
  245. return n;
  246. }
  247. static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
  248. loff_t *ppos)
  249. {
  250. struct hppfs_private *hppfs = file->private_data;
  251. struct hppfs_data *data;
  252. loff_t off;
  253. int err;
  254. if (hppfs->contents != NULL) {
  255. if (*ppos >= hppfs->len)
  256. return 0;
  257. data = hppfs->contents;
  258. off = *ppos;
  259. while (off >= sizeof(data->contents)) {
  260. data = list_entry(data->list.next, struct hppfs_data,
  261. list);
  262. off -= sizeof(data->contents);
  263. }
  264. if (off + count > hppfs->len)
  265. count = hppfs->len - off;
  266. copy_to_user(buf, &data->contents[off], count);
  267. *ppos += count;
  268. } else if (hppfs->host_fd != -1) {
  269. err = os_seek_file(hppfs->host_fd, *ppos);
  270. if (err) {
  271. printk(KERN_ERR "hppfs_read : seek failed, "
  272. "errno = %d\n", err);
  273. return err;
  274. }
  275. count = hppfs_read_file(hppfs->host_fd, buf, count);
  276. if (count > 0)
  277. *ppos += count;
  278. }
  279. else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
  280. return count;
  281. }
  282. static ssize_t hppfs_write(struct file *file, const char __user *buf, size_t len,
  283. loff_t *ppos)
  284. {
  285. struct hppfs_private *data = file->private_data;
  286. struct file *proc_file = data->proc_file;
  287. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  288. int err;
  289. write = proc_file->f_path.dentry->d_inode->i_fop->write;
  290. proc_file->f_pos = file->f_pos;
  291. err = (*write)(proc_file, buf, len, &proc_file->f_pos);
  292. file->f_pos = proc_file->f_pos;
  293. return err;
  294. }
  295. static int open_host_sock(char *host_file, int *filter_out)
  296. {
  297. char *end;
  298. int fd;
  299. end = &host_file[strlen(host_file)];
  300. strcpy(end, "/rw");
  301. *filter_out = 1;
  302. fd = os_connect_socket(host_file);
  303. if (fd > 0)
  304. return fd;
  305. strcpy(end, "/r");
  306. *filter_out = 0;
  307. fd = os_connect_socket(host_file);
  308. return fd;
  309. }
  310. static void free_contents(struct hppfs_data *head)
  311. {
  312. struct hppfs_data *data;
  313. struct list_head *ele, *next;
  314. if (head == NULL)
  315. return;
  316. list_for_each_safe(ele, next, &head->list) {
  317. data = list_entry(ele, struct hppfs_data, list);
  318. kfree(data);
  319. }
  320. kfree(head);
  321. }
  322. static struct hppfs_data *hppfs_get_data(int fd, int filter,
  323. struct file *proc_file,
  324. struct file *hppfs_file,
  325. loff_t *size_out)
  326. {
  327. struct hppfs_data *data, *new, *head;
  328. int n, err;
  329. err = -ENOMEM;
  330. data = kmalloc(sizeof(*data), GFP_KERNEL);
  331. if (data == NULL) {
  332. printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
  333. goto failed;
  334. }
  335. INIT_LIST_HEAD(&data->list);
  336. head = data;
  337. *size_out = 0;
  338. if (filter) {
  339. while ((n = read_proc(proc_file, data->contents,
  340. sizeof(data->contents), NULL, 0)) > 0)
  341. os_write_file(fd, data->contents, n);
  342. err = os_shutdown_socket(fd, 0, 1);
  343. if (err) {
  344. printk(KERN_ERR "hppfs_get_data : failed to shut down "
  345. "socket\n");
  346. goto failed_free;
  347. }
  348. }
  349. while (1) {
  350. n = os_read_file(fd, data->contents, sizeof(data->contents));
  351. if (n < 0) {
  352. err = n;
  353. printk(KERN_ERR "hppfs_get_data : read failed, "
  354. "errno = %d\n", err);
  355. goto failed_free;
  356. } else if (n == 0)
  357. break;
  358. *size_out += n;
  359. if (n < sizeof(data->contents))
  360. break;
  361. new = kmalloc(sizeof(*data), GFP_KERNEL);
  362. if (new == 0) {
  363. printk(KERN_ERR "hppfs_get_data : data allocation "
  364. "failed\n");
  365. err = -ENOMEM;
  366. goto failed_free;
  367. }
  368. INIT_LIST_HEAD(&new->list);
  369. list_add(&new->list, &data->list);
  370. data = new;
  371. }
  372. return head;
  373. failed_free:
  374. free_contents(head);
  375. failed:
  376. return ERR_PTR(err);
  377. }
  378. static struct hppfs_private *hppfs_data(void)
  379. {
  380. struct hppfs_private *data;
  381. data = kmalloc(sizeof(*data), GFP_KERNEL);
  382. if (data == NULL)
  383. return data;
  384. *data = ((struct hppfs_private ) { .host_fd = -1,
  385. .len = -1,
  386. .contents = NULL } );
  387. return data;
  388. }
  389. static int file_mode(int fmode)
  390. {
  391. if (fmode == (FMODE_READ | FMODE_WRITE))
  392. return O_RDWR;
  393. if (fmode == FMODE_READ)
  394. return O_RDONLY;
  395. if (fmode == FMODE_WRITE)
  396. return O_WRONLY;
  397. return 0;
  398. }
  399. static int hppfs_open(struct inode *inode, struct file *file)
  400. {
  401. struct hppfs_private *data;
  402. struct dentry *proc_dentry;
  403. struct vfsmount *proc_mnt;
  404. char *host_file;
  405. int err, fd, type, filter;
  406. err = -ENOMEM;
  407. data = hppfs_data();
  408. if (data == NULL)
  409. goto out;
  410. host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
  411. if (host_file == NULL)
  412. goto out_free2;
  413. proc_dentry = HPPFS_I(inode)->proc_dentry;
  414. proc_mnt = HPPFS_I(inode)->proc_mnt;
  415. /* XXX This isn't closed anywhere */
  416. data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
  417. file_mode(file->f_mode));
  418. err = PTR_ERR(data->proc_file);
  419. if (IS_ERR(data->proc_file))
  420. goto out_free1;
  421. type = os_file_type(host_file);
  422. if (type == OS_TYPE_FILE) {
  423. fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
  424. if (fd >= 0)
  425. data->host_fd = fd;
  426. else
  427. printk(KERN_ERR "hppfs_open : failed to open '%s', "
  428. "errno = %d\n", host_file, -fd);
  429. data->contents = NULL;
  430. } else if (type == OS_TYPE_DIR) {
  431. fd = open_host_sock(host_file, &filter);
  432. if (fd > 0) {
  433. data->contents = hppfs_get_data(fd, filter,
  434. data->proc_file,
  435. file, &data->len);
  436. if (!IS_ERR(data->contents))
  437. data->host_fd = fd;
  438. } else
  439. printk(KERN_ERR "hppfs_open : failed to open a socket "
  440. "in '%s', errno = %d\n", host_file, -fd);
  441. }
  442. kfree(host_file);
  443. file->private_data = data;
  444. return 0;
  445. out_free1:
  446. kfree(host_file);
  447. out_free2:
  448. free_contents(data->contents);
  449. kfree(data);
  450. out:
  451. return err;
  452. }
  453. static int hppfs_dir_open(struct inode *inode, struct file *file)
  454. {
  455. struct hppfs_private *data;
  456. struct dentry *proc_dentry;
  457. struct vfsmount *proc_mnt;
  458. int err;
  459. err = -ENOMEM;
  460. data = hppfs_data();
  461. if (data == NULL)
  462. goto out;
  463. proc_dentry = HPPFS_I(inode)->proc_dentry;
  464. proc_mnt = HPPFS_I(inode)->proc_mnt;
  465. data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
  466. file_mode(file->f_mode));
  467. err = PTR_ERR(data->proc_file);
  468. if (IS_ERR(data->proc_file))
  469. goto out_free;
  470. file->private_data = data;
  471. return 0;
  472. out_free:
  473. kfree(data);
  474. out:
  475. return err;
  476. }
  477. static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
  478. {
  479. struct hppfs_private *data = file->private_data;
  480. struct file *proc_file = data->proc_file;
  481. loff_t (*llseek)(struct file *, loff_t, int);
  482. loff_t ret;
  483. llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
  484. if (llseek != NULL) {
  485. ret = (*llseek)(proc_file, off, where);
  486. if (ret < 0)
  487. return ret;
  488. }
  489. return default_llseek(file, off, where);
  490. }
  491. static const struct file_operations hppfs_file_fops = {
  492. .owner = NULL,
  493. .llseek = hppfs_llseek,
  494. .read = hppfs_read,
  495. .write = hppfs_write,
  496. .open = hppfs_open,
  497. };
  498. struct hppfs_dirent {
  499. void *vfs_dirent;
  500. filldir_t filldir;
  501. struct dentry *dentry;
  502. };
  503. static int hppfs_filldir(void *d, const char *name, int size,
  504. loff_t offset, u64 inode, unsigned int type)
  505. {
  506. struct hppfs_dirent *dirent = d;
  507. if (file_removed(dirent->dentry, name))
  508. return 0;
  509. return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
  510. inode, type);
  511. }
  512. static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
  513. {
  514. struct hppfs_private *data = file->private_data;
  515. struct file *proc_file = data->proc_file;
  516. int (*readdir)(struct file *, void *, filldir_t);
  517. struct hppfs_dirent dirent = ((struct hppfs_dirent)
  518. { .vfs_dirent = ent,
  519. .filldir = filldir,
  520. .dentry = file->f_path.dentry
  521. });
  522. int err;
  523. readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
  524. proc_file->f_pos = file->f_pos;
  525. err = (*readdir)(proc_file, &dirent, hppfs_filldir);
  526. file->f_pos = proc_file->f_pos;
  527. return err;
  528. }
  529. static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  530. {
  531. return 0;
  532. }
  533. static const struct file_operations hppfs_dir_fops = {
  534. .owner = NULL,
  535. .readdir = hppfs_readdir,
  536. .open = hppfs_dir_open,
  537. .fsync = hppfs_fsync,
  538. };
  539. static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  540. {
  541. sf->f_blocks = 0;
  542. sf->f_bfree = 0;
  543. sf->f_bavail = 0;
  544. sf->f_files = 0;
  545. sf->f_ffree = 0;
  546. sf->f_type = HPPFS_SUPER_MAGIC;
  547. return 0;
  548. }
  549. static struct inode *hppfs_alloc_inode(struct super_block *sb)
  550. {
  551. struct hppfs_inode_info *hi;
  552. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  553. if (!hi)
  554. return NULL;
  555. hi->proc_dentry = NULL;
  556. hi->proc_mnt = NULL;
  557. inode_init_once(&hi->vfs_inode);
  558. return &hi->vfs_inode;
  559. }
  560. void hppfs_delete_inode(struct inode *ino)
  561. {
  562. clear_inode(ino);
  563. }
  564. static void hppfs_destroy_inode(struct inode *inode)
  565. {
  566. kfree(HPPFS_I(inode));
  567. }
  568. static void hppfs_put_super(struct super_block *sb)
  569. {
  570. mntput(HPPFS_I(sb->s_root->d_inode)->proc_mnt);
  571. }
  572. static const struct super_operations hppfs_sbops = {
  573. .alloc_inode = hppfs_alloc_inode,
  574. .destroy_inode = hppfs_destroy_inode,
  575. .delete_inode = hppfs_delete_inode,
  576. .statfs = hppfs_statfs,
  577. .put_super = hppfs_put_super,
  578. };
  579. static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
  580. int buflen)
  581. {
  582. struct file *proc_file;
  583. struct dentry *proc_dentry;
  584. struct vfsmount *proc_mnt;
  585. int ret;
  586. proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
  587. proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
  588. proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
  589. if (IS_ERR(proc_file))
  590. return PTR_ERR(proc_file);
  591. ret = proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, buflen);
  592. fput(proc_file);
  593. return ret;
  594. }
  595. static void* hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  596. {
  597. struct file *proc_file;
  598. struct dentry *proc_dentry;
  599. struct vfsmount *proc_mnt;
  600. void *ret;
  601. proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
  602. proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
  603. proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
  604. if (IS_ERR(proc_file))
  605. return proc_file;
  606. ret = proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
  607. fput(proc_file);
  608. return ret;
  609. }
  610. static const struct inode_operations hppfs_dir_iops = {
  611. .lookup = hppfs_lookup,
  612. };
  613. static const struct inode_operations hppfs_link_iops = {
  614. .readlink = hppfs_readlink,
  615. .follow_link = hppfs_follow_link,
  616. };
  617. static int init_inode(struct inode *inode, struct dentry *dentry,
  618. struct vfsmount *mnt)
  619. {
  620. if (S_ISDIR(dentry->d_inode->i_mode)) {
  621. inode->i_op = &hppfs_dir_iops;
  622. inode->i_fop = &hppfs_dir_fops;
  623. } else if (S_ISLNK(dentry->d_inode->i_mode)) {
  624. inode->i_op = &hppfs_link_iops;
  625. inode->i_fop = &hppfs_file_fops;
  626. } else {
  627. inode->i_op = &hppfs_file_iops;
  628. inode->i_fop = &hppfs_file_fops;
  629. }
  630. HPPFS_I(inode)->proc_dentry = dentry;
  631. HPPFS_I(inode)->proc_mnt = mnt;
  632. return 0;
  633. }
  634. static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
  635. {
  636. struct inode *root_inode;
  637. struct file_system_type *procfs;
  638. struct vfsmount *proc_mnt;
  639. int err;
  640. err = -ENOENT;
  641. procfs = get_fs_type("proc");
  642. if (!procfs)
  643. goto out;
  644. proc_mnt = vfs_kern_mount(procfs, 0, procfs->name, NULL);
  645. put_filesystem(procfs);
  646. if (IS_ERR(proc_mnt))
  647. goto out;
  648. sb->s_blocksize = 1024;
  649. sb->s_blocksize_bits = 10;
  650. sb->s_magic = HPPFS_SUPER_MAGIC;
  651. sb->s_op = &hppfs_sbops;
  652. root_inode = hppfs_iget(sb);
  653. if (IS_ERR(root_inode)) {
  654. err = PTR_ERR(root_inode);
  655. goto out;
  656. }
  657. err = init_inode(root_inode, proc_mnt->mnt_sb->s_root, proc_mnt);
  658. if (err)
  659. goto out_iput;
  660. err = -ENOMEM;
  661. sb->s_root = d_alloc_root(root_inode);
  662. if (!sb->s_root)
  663. goto out_iput;
  664. hppfs_read_inode(root_inode);
  665. return 0;
  666. out_iput:
  667. iput(root_inode);
  668. out_mntput:
  669. mntput(proc_mnt);
  670. out:
  671. return(err);
  672. }
  673. static int hppfs_read_super(struct file_system_type *type,
  674. int flags, const char *dev_name,
  675. void *data, struct vfsmount *mnt)
  676. {
  677. return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
  678. }
  679. static struct file_system_type hppfs_type = {
  680. .owner = THIS_MODULE,
  681. .name = "hppfs",
  682. .get_sb = hppfs_read_super,
  683. .kill_sb = kill_anon_super,
  684. .fs_flags = 0,
  685. };
  686. static int __init init_hppfs(void)
  687. {
  688. return register_filesystem(&hppfs_type);
  689. }
  690. static void __exit exit_hppfs(void)
  691. {
  692. unregister_filesystem(&hppfs_type);
  693. }
  694. module_init(init_hppfs)
  695. module_exit(exit_hppfs)
  696. MODULE_LICENSE("GPL");