rpc_pipe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * net/sunrpc/rpc_pipe.c
  3. *
  4. * Userland/kernel interface for rpcauth_gss.
  5. * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
  6. * and fs/sysfs/inode.c
  7. *
  8. * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
  9. *
  10. */
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/dnotify.h>
  19. #include <linux/kernel.h>
  20. #include <asm/ioctls.h>
  21. #include <linux/fs.h>
  22. #include <linux/poll.h>
  23. #include <linux/wait.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/sunrpc/rpc_pipe_fs.h>
  28. static struct vfsmount *rpc_mount __read_mostly;
  29. static int rpc_mount_count;
  30. static struct file_system_type rpc_pipe_fs_type;
  31. static kmem_cache_t *rpc_inode_cachep __read_mostly;
  32. #define RPC_UPCALL_TIMEOUT (30*HZ)
  33. static void
  34. __rpc_purge_upcall(struct inode *inode, int err)
  35. {
  36. struct rpc_inode *rpci = RPC_I(inode);
  37. struct rpc_pipe_msg *msg;
  38. while (!list_empty(&rpci->pipe)) {
  39. msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
  40. list_del_init(&msg->list);
  41. msg->errno = err;
  42. rpci->ops->destroy_msg(msg);
  43. }
  44. while (!list_empty(&rpci->in_upcall)) {
  45. msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
  46. list_del_init(&msg->list);
  47. msg->errno = err;
  48. rpci->ops->destroy_msg(msg);
  49. }
  50. rpci->pipelen = 0;
  51. wake_up(&rpci->waitq);
  52. }
  53. static void
  54. rpc_timeout_upcall_queue(void *data)
  55. {
  56. struct rpc_inode *rpci = (struct rpc_inode *)data;
  57. struct inode *inode = &rpci->vfs_inode;
  58. down(&inode->i_sem);
  59. if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
  60. __rpc_purge_upcall(inode, -ETIMEDOUT);
  61. up(&inode->i_sem);
  62. }
  63. int
  64. rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
  65. {
  66. struct rpc_inode *rpci = RPC_I(inode);
  67. int res = -EPIPE;
  68. down(&inode->i_sem);
  69. if (rpci->ops == NULL)
  70. goto out;
  71. if (rpci->nreaders) {
  72. list_add_tail(&msg->list, &rpci->pipe);
  73. rpci->pipelen += msg->len;
  74. res = 0;
  75. } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  76. if (list_empty(&rpci->pipe))
  77. schedule_delayed_work(&rpci->queue_timeout,
  78. RPC_UPCALL_TIMEOUT);
  79. list_add_tail(&msg->list, &rpci->pipe);
  80. rpci->pipelen += msg->len;
  81. res = 0;
  82. }
  83. out:
  84. up(&inode->i_sem);
  85. wake_up(&rpci->waitq);
  86. return res;
  87. }
  88. static inline void
  89. rpc_inode_setowner(struct inode *inode, void *private)
  90. {
  91. RPC_I(inode)->private = private;
  92. }
  93. static void
  94. rpc_close_pipes(struct inode *inode)
  95. {
  96. struct rpc_inode *rpci = RPC_I(inode);
  97. cancel_delayed_work(&rpci->queue_timeout);
  98. flush_scheduled_work();
  99. down(&inode->i_sem);
  100. if (rpci->ops != NULL) {
  101. rpci->nreaders = 0;
  102. __rpc_purge_upcall(inode, -EPIPE);
  103. rpci->nwriters = 0;
  104. if (rpci->ops->release_pipe)
  105. rpci->ops->release_pipe(inode);
  106. rpci->ops = NULL;
  107. }
  108. rpc_inode_setowner(inode, NULL);
  109. up(&inode->i_sem);
  110. }
  111. static struct inode *
  112. rpc_alloc_inode(struct super_block *sb)
  113. {
  114. struct rpc_inode *rpci;
  115. rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
  116. if (!rpci)
  117. return NULL;
  118. return &rpci->vfs_inode;
  119. }
  120. static void
  121. rpc_destroy_inode(struct inode *inode)
  122. {
  123. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  124. }
  125. static int
  126. rpc_pipe_open(struct inode *inode, struct file *filp)
  127. {
  128. struct rpc_inode *rpci = RPC_I(inode);
  129. int res = -ENXIO;
  130. down(&inode->i_sem);
  131. if (rpci->ops != NULL) {
  132. if (filp->f_mode & FMODE_READ)
  133. rpci->nreaders ++;
  134. if (filp->f_mode & FMODE_WRITE)
  135. rpci->nwriters ++;
  136. res = 0;
  137. }
  138. up(&inode->i_sem);
  139. return res;
  140. }
  141. static int
  142. rpc_pipe_release(struct inode *inode, struct file *filp)
  143. {
  144. struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
  145. struct rpc_pipe_msg *msg;
  146. down(&inode->i_sem);
  147. if (rpci->ops == NULL)
  148. goto out;
  149. msg = (struct rpc_pipe_msg *)filp->private_data;
  150. if (msg != NULL) {
  151. msg->errno = -EPIPE;
  152. list_del_init(&msg->list);
  153. rpci->ops->destroy_msg(msg);
  154. }
  155. if (filp->f_mode & FMODE_WRITE)
  156. rpci->nwriters --;
  157. if (filp->f_mode & FMODE_READ)
  158. rpci->nreaders --;
  159. if (!rpci->nreaders)
  160. __rpc_purge_upcall(inode, -EPIPE);
  161. if (rpci->ops->release_pipe)
  162. rpci->ops->release_pipe(inode);
  163. out:
  164. up(&inode->i_sem);
  165. return 0;
  166. }
  167. static ssize_t
  168. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  169. {
  170. struct inode *inode = filp->f_dentry->d_inode;
  171. struct rpc_inode *rpci = RPC_I(inode);
  172. struct rpc_pipe_msg *msg;
  173. int res = 0;
  174. down(&inode->i_sem);
  175. if (rpci->ops == NULL) {
  176. res = -EPIPE;
  177. goto out_unlock;
  178. }
  179. msg = filp->private_data;
  180. if (msg == NULL) {
  181. if (!list_empty(&rpci->pipe)) {
  182. msg = list_entry(rpci->pipe.next,
  183. struct rpc_pipe_msg,
  184. list);
  185. list_move(&msg->list, &rpci->in_upcall);
  186. rpci->pipelen -= msg->len;
  187. filp->private_data = msg;
  188. msg->copied = 0;
  189. }
  190. if (msg == NULL)
  191. goto out_unlock;
  192. }
  193. /* NOTE: it is up to the callback to update msg->copied */
  194. res = rpci->ops->upcall(filp, msg, buf, len);
  195. if (res < 0 || msg->len == msg->copied) {
  196. filp->private_data = NULL;
  197. list_del_init(&msg->list);
  198. rpci->ops->destroy_msg(msg);
  199. }
  200. out_unlock:
  201. up(&inode->i_sem);
  202. return res;
  203. }
  204. static ssize_t
  205. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  206. {
  207. struct inode *inode = filp->f_dentry->d_inode;
  208. struct rpc_inode *rpci = RPC_I(inode);
  209. int res;
  210. down(&inode->i_sem);
  211. res = -EPIPE;
  212. if (rpci->ops != NULL)
  213. res = rpci->ops->downcall(filp, buf, len);
  214. up(&inode->i_sem);
  215. return res;
  216. }
  217. static unsigned int
  218. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  219. {
  220. struct rpc_inode *rpci;
  221. unsigned int mask = 0;
  222. rpci = RPC_I(filp->f_dentry->d_inode);
  223. poll_wait(filp, &rpci->waitq, wait);
  224. mask = POLLOUT | POLLWRNORM;
  225. if (rpci->ops == NULL)
  226. mask |= POLLERR | POLLHUP;
  227. if (!list_empty(&rpci->pipe))
  228. mask |= POLLIN | POLLRDNORM;
  229. return mask;
  230. }
  231. static int
  232. rpc_pipe_ioctl(struct inode *ino, struct file *filp,
  233. unsigned int cmd, unsigned long arg)
  234. {
  235. struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
  236. int len;
  237. switch (cmd) {
  238. case FIONREAD:
  239. if (rpci->ops == NULL)
  240. return -EPIPE;
  241. len = rpci->pipelen;
  242. if (filp->private_data) {
  243. struct rpc_pipe_msg *msg;
  244. msg = (struct rpc_pipe_msg *)filp->private_data;
  245. len += msg->len - msg->copied;
  246. }
  247. return put_user(len, (int __user *)arg);
  248. default:
  249. return -EINVAL;
  250. }
  251. }
  252. static struct file_operations rpc_pipe_fops = {
  253. .owner = THIS_MODULE,
  254. .llseek = no_llseek,
  255. .read = rpc_pipe_read,
  256. .write = rpc_pipe_write,
  257. .poll = rpc_pipe_poll,
  258. .ioctl = rpc_pipe_ioctl,
  259. .open = rpc_pipe_open,
  260. .release = rpc_pipe_release,
  261. };
  262. static int
  263. rpc_show_info(struct seq_file *m, void *v)
  264. {
  265. struct rpc_clnt *clnt = m->private;
  266. seq_printf(m, "RPC server: %s\n", clnt->cl_server);
  267. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
  268. clnt->cl_prog, clnt->cl_vers);
  269. seq_printf(m, "address: %u.%u.%u.%u\n",
  270. NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
  271. seq_printf(m, "protocol: %s\n",
  272. clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
  273. return 0;
  274. }
  275. static int
  276. rpc_info_open(struct inode *inode, struct file *file)
  277. {
  278. struct rpc_clnt *clnt;
  279. int ret = single_open(file, rpc_show_info, NULL);
  280. if (!ret) {
  281. struct seq_file *m = file->private_data;
  282. down(&inode->i_sem);
  283. clnt = RPC_I(inode)->private;
  284. if (clnt) {
  285. atomic_inc(&clnt->cl_users);
  286. m->private = clnt;
  287. } else {
  288. single_release(inode, file);
  289. ret = -EINVAL;
  290. }
  291. up(&inode->i_sem);
  292. }
  293. return ret;
  294. }
  295. static int
  296. rpc_info_release(struct inode *inode, struct file *file)
  297. {
  298. struct seq_file *m = file->private_data;
  299. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  300. if (clnt)
  301. rpc_release_client(clnt);
  302. return single_release(inode, file);
  303. }
  304. static struct file_operations rpc_info_operations = {
  305. .owner = THIS_MODULE,
  306. .open = rpc_info_open,
  307. .read = seq_read,
  308. .llseek = seq_lseek,
  309. .release = rpc_info_release,
  310. };
  311. /*
  312. * We have a single directory with 1 node in it.
  313. */
  314. enum {
  315. RPCAUTH_Root = 1,
  316. RPCAUTH_lockd,
  317. RPCAUTH_mount,
  318. RPCAUTH_nfs,
  319. RPCAUTH_portmap,
  320. RPCAUTH_statd,
  321. RPCAUTH_RootEOF
  322. };
  323. /*
  324. * Description of fs contents.
  325. */
  326. struct rpc_filelist {
  327. char *name;
  328. struct file_operations *i_fop;
  329. int mode;
  330. };
  331. static struct rpc_filelist files[] = {
  332. [RPCAUTH_lockd] = {
  333. .name = "lockd",
  334. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  335. },
  336. [RPCAUTH_mount] = {
  337. .name = "mount",
  338. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  339. },
  340. [RPCAUTH_nfs] = {
  341. .name = "nfs",
  342. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  343. },
  344. [RPCAUTH_portmap] = {
  345. .name = "portmap",
  346. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  347. },
  348. [RPCAUTH_statd] = {
  349. .name = "statd",
  350. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  351. },
  352. };
  353. enum {
  354. RPCAUTH_info = 2,
  355. RPCAUTH_EOF
  356. };
  357. static struct rpc_filelist authfiles[] = {
  358. [RPCAUTH_info] = {
  359. .name = "info",
  360. .i_fop = &rpc_info_operations,
  361. .mode = S_IFREG | S_IRUSR,
  362. },
  363. };
  364. static int
  365. rpc_get_mount(void)
  366. {
  367. return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
  368. }
  369. static void
  370. rpc_put_mount(void)
  371. {
  372. simple_release_fs(&rpc_mount, &rpc_mount_count);
  373. }
  374. static int
  375. rpc_lookup_parent(char *path, struct nameidata *nd)
  376. {
  377. if (path[0] == '\0')
  378. return -ENOENT;
  379. if (rpc_get_mount()) {
  380. printk(KERN_WARNING "%s: %s failed to mount "
  381. "pseudofilesystem \n", __FILE__, __FUNCTION__);
  382. return -ENODEV;
  383. }
  384. nd->mnt = mntget(rpc_mount);
  385. nd->dentry = dget(rpc_mount->mnt_root);
  386. nd->last_type = LAST_ROOT;
  387. nd->flags = LOOKUP_PARENT;
  388. nd->depth = 0;
  389. if (path_walk(path, nd)) {
  390. printk(KERN_WARNING "%s: %s failed to find path %s\n",
  391. __FILE__, __FUNCTION__, path);
  392. rpc_put_mount();
  393. return -ENOENT;
  394. }
  395. return 0;
  396. }
  397. static void
  398. rpc_release_path(struct nameidata *nd)
  399. {
  400. path_release(nd);
  401. rpc_put_mount();
  402. }
  403. static struct inode *
  404. rpc_get_inode(struct super_block *sb, int mode)
  405. {
  406. struct inode *inode = new_inode(sb);
  407. if (!inode)
  408. return NULL;
  409. inode->i_mode = mode;
  410. inode->i_uid = inode->i_gid = 0;
  411. inode->i_blksize = PAGE_CACHE_SIZE;
  412. inode->i_blocks = 0;
  413. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  414. switch(mode & S_IFMT) {
  415. case S_IFDIR:
  416. inode->i_fop = &simple_dir_operations;
  417. inode->i_op = &simple_dir_inode_operations;
  418. inode->i_nlink++;
  419. default:
  420. break;
  421. }
  422. return inode;
  423. }
  424. /*
  425. * FIXME: This probably has races.
  426. */
  427. static void
  428. rpc_depopulate(struct dentry *parent)
  429. {
  430. struct inode *dir = parent->d_inode;
  431. struct list_head *pos, *next;
  432. struct dentry *dentry, *dvec[10];
  433. int n = 0;
  434. down(&dir->i_sem);
  435. repeat:
  436. spin_lock(&dcache_lock);
  437. list_for_each_safe(pos, next, &parent->d_subdirs) {
  438. dentry = list_entry(pos, struct dentry, d_child);
  439. spin_lock(&dentry->d_lock);
  440. if (!d_unhashed(dentry)) {
  441. dget_locked(dentry);
  442. __d_drop(dentry);
  443. spin_unlock(&dentry->d_lock);
  444. dvec[n++] = dentry;
  445. if (n == ARRAY_SIZE(dvec))
  446. break;
  447. } else
  448. spin_unlock(&dentry->d_lock);
  449. }
  450. spin_unlock(&dcache_lock);
  451. if (n) {
  452. do {
  453. dentry = dvec[--n];
  454. if (dentry->d_inode) {
  455. rpc_close_pipes(dentry->d_inode);
  456. simple_unlink(dir, dentry);
  457. }
  458. dput(dentry);
  459. } while (n);
  460. goto repeat;
  461. }
  462. up(&dir->i_sem);
  463. }
  464. static int
  465. rpc_populate(struct dentry *parent,
  466. struct rpc_filelist *files,
  467. int start, int eof)
  468. {
  469. struct inode *inode, *dir = parent->d_inode;
  470. void *private = RPC_I(dir)->private;
  471. struct dentry *dentry;
  472. int mode, i;
  473. down(&dir->i_sem);
  474. for (i = start; i < eof; i++) {
  475. dentry = d_alloc_name(parent, files[i].name);
  476. if (!dentry)
  477. goto out_bad;
  478. mode = files[i].mode;
  479. inode = rpc_get_inode(dir->i_sb, mode);
  480. if (!inode) {
  481. dput(dentry);
  482. goto out_bad;
  483. }
  484. inode->i_ino = i;
  485. if (files[i].i_fop)
  486. inode->i_fop = files[i].i_fop;
  487. if (private)
  488. rpc_inode_setowner(inode, private);
  489. if (S_ISDIR(mode))
  490. dir->i_nlink++;
  491. d_add(dentry, inode);
  492. }
  493. up(&dir->i_sem);
  494. return 0;
  495. out_bad:
  496. up(&dir->i_sem);
  497. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  498. __FILE__, __FUNCTION__, parent->d_name.name);
  499. return -ENOMEM;
  500. }
  501. static int
  502. __rpc_mkdir(struct inode *dir, struct dentry *dentry)
  503. {
  504. struct inode *inode;
  505. inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
  506. if (!inode)
  507. goto out_err;
  508. inode->i_ino = iunique(dir->i_sb, 100);
  509. d_instantiate(dentry, inode);
  510. dir->i_nlink++;
  511. inode_dir_notify(dir, DN_CREATE);
  512. rpc_get_mount();
  513. return 0;
  514. out_err:
  515. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  516. __FILE__, __FUNCTION__, dentry->d_name.name);
  517. return -ENOMEM;
  518. }
  519. static int
  520. __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  521. {
  522. int error;
  523. shrink_dcache_parent(dentry);
  524. if (dentry->d_inode)
  525. rpc_close_pipes(dentry->d_inode);
  526. if ((error = simple_rmdir(dir, dentry)) != 0)
  527. return error;
  528. if (!error) {
  529. inode_dir_notify(dir, DN_DELETE);
  530. d_drop(dentry);
  531. rpc_put_mount();
  532. }
  533. return 0;
  534. }
  535. static struct dentry *
  536. rpc_lookup_negative(char *path, struct nameidata *nd)
  537. {
  538. struct dentry *dentry;
  539. struct inode *dir;
  540. int error;
  541. if ((error = rpc_lookup_parent(path, nd)) != 0)
  542. return ERR_PTR(error);
  543. dir = nd->dentry->d_inode;
  544. down(&dir->i_sem);
  545. dentry = lookup_hash(nd);
  546. if (IS_ERR(dentry))
  547. goto out_err;
  548. if (dentry->d_inode) {
  549. dput(dentry);
  550. dentry = ERR_PTR(-EEXIST);
  551. goto out_err;
  552. }
  553. return dentry;
  554. out_err:
  555. up(&dir->i_sem);
  556. rpc_release_path(nd);
  557. return dentry;
  558. }
  559. struct dentry *
  560. rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
  561. {
  562. struct nameidata nd;
  563. struct dentry *dentry;
  564. struct inode *dir;
  565. int error;
  566. dentry = rpc_lookup_negative(path, &nd);
  567. if (IS_ERR(dentry))
  568. return dentry;
  569. dir = nd.dentry->d_inode;
  570. if ((error = __rpc_mkdir(dir, dentry)) != 0)
  571. goto err_dput;
  572. RPC_I(dentry->d_inode)->private = rpc_client;
  573. error = rpc_populate(dentry, authfiles,
  574. RPCAUTH_info, RPCAUTH_EOF);
  575. if (error)
  576. goto err_depopulate;
  577. out:
  578. up(&dir->i_sem);
  579. rpc_release_path(&nd);
  580. return dentry;
  581. err_depopulate:
  582. rpc_depopulate(dentry);
  583. __rpc_rmdir(dir, dentry);
  584. err_dput:
  585. dput(dentry);
  586. printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
  587. __FILE__, __FUNCTION__, path, error);
  588. dentry = ERR_PTR(error);
  589. goto out;
  590. }
  591. int
  592. rpc_rmdir(char *path)
  593. {
  594. struct nameidata nd;
  595. struct dentry *dentry;
  596. struct inode *dir;
  597. int error;
  598. if ((error = rpc_lookup_parent(path, &nd)) != 0)
  599. return error;
  600. dir = nd.dentry->d_inode;
  601. down(&dir->i_sem);
  602. dentry = lookup_hash(&nd);
  603. if (IS_ERR(dentry)) {
  604. error = PTR_ERR(dentry);
  605. goto out_release;
  606. }
  607. rpc_depopulate(dentry);
  608. error = __rpc_rmdir(dir, dentry);
  609. dput(dentry);
  610. out_release:
  611. up(&dir->i_sem);
  612. rpc_release_path(&nd);
  613. return error;
  614. }
  615. struct dentry *
  616. rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
  617. {
  618. struct nameidata nd;
  619. struct dentry *dentry;
  620. struct inode *dir, *inode;
  621. struct rpc_inode *rpci;
  622. dentry = rpc_lookup_negative(path, &nd);
  623. if (IS_ERR(dentry))
  624. return dentry;
  625. dir = nd.dentry->d_inode;
  626. inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
  627. if (!inode)
  628. goto err_dput;
  629. inode->i_ino = iunique(dir->i_sb, 100);
  630. inode->i_fop = &rpc_pipe_fops;
  631. d_instantiate(dentry, inode);
  632. rpci = RPC_I(inode);
  633. rpci->private = private;
  634. rpci->flags = flags;
  635. rpci->ops = ops;
  636. inode_dir_notify(dir, DN_CREATE);
  637. out:
  638. up(&dir->i_sem);
  639. rpc_release_path(&nd);
  640. return dentry;
  641. err_dput:
  642. dput(dentry);
  643. dentry = ERR_PTR(-ENOMEM);
  644. printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
  645. __FILE__, __FUNCTION__, path, -ENOMEM);
  646. goto out;
  647. }
  648. int
  649. rpc_unlink(char *path)
  650. {
  651. struct nameidata nd;
  652. struct dentry *dentry;
  653. struct inode *dir;
  654. int error;
  655. if ((error = rpc_lookup_parent(path, &nd)) != 0)
  656. return error;
  657. dir = nd.dentry->d_inode;
  658. down(&dir->i_sem);
  659. dentry = lookup_hash(&nd);
  660. if (IS_ERR(dentry)) {
  661. error = PTR_ERR(dentry);
  662. goto out_release;
  663. }
  664. d_drop(dentry);
  665. if (dentry->d_inode) {
  666. rpc_close_pipes(dentry->d_inode);
  667. error = simple_unlink(dir, dentry);
  668. }
  669. dput(dentry);
  670. inode_dir_notify(dir, DN_DELETE);
  671. out_release:
  672. up(&dir->i_sem);
  673. rpc_release_path(&nd);
  674. return error;
  675. }
  676. /*
  677. * populate the filesystem
  678. */
  679. static struct super_operations s_ops = {
  680. .alloc_inode = rpc_alloc_inode,
  681. .destroy_inode = rpc_destroy_inode,
  682. .statfs = simple_statfs,
  683. };
  684. #define RPCAUTH_GSSMAGIC 0x67596969
  685. static int
  686. rpc_fill_super(struct super_block *sb, void *data, int silent)
  687. {
  688. struct inode *inode;
  689. struct dentry *root;
  690. sb->s_blocksize = PAGE_CACHE_SIZE;
  691. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  692. sb->s_magic = RPCAUTH_GSSMAGIC;
  693. sb->s_op = &s_ops;
  694. sb->s_time_gran = 1;
  695. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  696. if (!inode)
  697. return -ENOMEM;
  698. root = d_alloc_root(inode);
  699. if (!root) {
  700. iput(inode);
  701. return -ENOMEM;
  702. }
  703. if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
  704. goto out;
  705. sb->s_root = root;
  706. return 0;
  707. out:
  708. d_genocide(root);
  709. dput(root);
  710. return -ENOMEM;
  711. }
  712. static struct super_block *
  713. rpc_get_sb(struct file_system_type *fs_type,
  714. int flags, const char *dev_name, void *data)
  715. {
  716. return get_sb_single(fs_type, flags, data, rpc_fill_super);
  717. }
  718. static struct file_system_type rpc_pipe_fs_type = {
  719. .owner = THIS_MODULE,
  720. .name = "rpc_pipefs",
  721. .get_sb = rpc_get_sb,
  722. .kill_sb = kill_litter_super,
  723. };
  724. static void
  725. init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  726. {
  727. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  728. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  729. SLAB_CTOR_CONSTRUCTOR) {
  730. inode_init_once(&rpci->vfs_inode);
  731. rpci->private = NULL;
  732. rpci->nreaders = 0;
  733. rpci->nwriters = 0;
  734. INIT_LIST_HEAD(&rpci->in_upcall);
  735. INIT_LIST_HEAD(&rpci->pipe);
  736. rpci->pipelen = 0;
  737. init_waitqueue_head(&rpci->waitq);
  738. INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
  739. rpci->ops = NULL;
  740. }
  741. }
  742. int register_rpc_pipefs(void)
  743. {
  744. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  745. sizeof(struct rpc_inode),
  746. 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
  747. init_once, NULL);
  748. if (!rpc_inode_cachep)
  749. return -ENOMEM;
  750. register_filesystem(&rpc_pipe_fs_type);
  751. return 0;
  752. }
  753. void unregister_rpc_pipefs(void)
  754. {
  755. if (kmem_cache_destroy(rpc_inode_cachep))
  756. printk(KERN_WARNING "RPC: unable to free inode cache\n");
  757. unregister_filesystem(&rpc_pipe_fs_type);
  758. }