rpc_pipe.c 19 KB

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