rpc_pipe.c 18 KB

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