rpc_pipe.c 18 KB

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