rpc_pipe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mount.h>
  16. #include <linux/namei.h>
  17. #include <linux/fsnotify.h>
  18. #include <linux/kernel.h>
  19. #include <asm/ioctls.h>
  20. #include <linux/fs.h>
  21. #include <linux/poll.h>
  22. #include <linux/wait.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/sunrpc/clnt.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/sunrpc/rpc_pipe_fs.h>
  27. static struct vfsmount *rpc_mount __read_mostly;
  28. static int rpc_mount_count;
  29. static struct file_system_type rpc_pipe_fs_type;
  30. static struct kmem_cache *rpc_inode_cachep __read_mostly;
  31. #define RPC_UPCALL_TIMEOUT (30*HZ)
  32. static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
  33. void (*destroy_msg)(struct rpc_pipe_msg *), int err)
  34. {
  35. struct rpc_pipe_msg *msg;
  36. if (list_empty(head))
  37. return;
  38. do {
  39. msg = list_entry(head->next, struct rpc_pipe_msg, list);
  40. list_del(&msg->list);
  41. msg->errno = err;
  42. destroy_msg(msg);
  43. } while (!list_empty(head));
  44. wake_up(&rpci->waitq);
  45. }
  46. static void
  47. rpc_timeout_upcall_queue(struct work_struct *work)
  48. {
  49. LIST_HEAD(free_list);
  50. struct rpc_inode *rpci =
  51. container_of(work, struct rpc_inode, queue_timeout.work);
  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. /**
  68. * rpc_queue_upcall
  69. * @inode: inode of upcall pipe on which to queue given message
  70. * @msg: message to queue
  71. *
  72. * Call with an @inode created by rpc_mkpipe() to queue an upcall.
  73. * A userspace process may then later read the upcall by performing a
  74. * read on an open file for this inode. It is up to the caller to
  75. * initialize the fields of @msg (other than @msg->list) appropriately.
  76. */
  77. int
  78. rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
  79. {
  80. struct rpc_inode *rpci = RPC_I(inode);
  81. int res = -EPIPE;
  82. spin_lock(&inode->i_lock);
  83. if (rpci->ops == NULL)
  84. goto out;
  85. if (rpci->nreaders) {
  86. list_add_tail(&msg->list, &rpci->pipe);
  87. rpci->pipelen += msg->len;
  88. res = 0;
  89. } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  90. if (list_empty(&rpci->pipe))
  91. queue_delayed_work(rpciod_workqueue,
  92. &rpci->queue_timeout,
  93. RPC_UPCALL_TIMEOUT);
  94. list_add_tail(&msg->list, &rpci->pipe);
  95. rpci->pipelen += msg->len;
  96. res = 0;
  97. }
  98. out:
  99. spin_unlock(&inode->i_lock);
  100. wake_up(&rpci->waitq);
  101. return res;
  102. }
  103. EXPORT_SYMBOL(rpc_queue_upcall);
  104. static inline void
  105. rpc_inode_setowner(struct inode *inode, void *private)
  106. {
  107. RPC_I(inode)->private = private;
  108. }
  109. static void
  110. rpc_close_pipes(struct inode *inode)
  111. {
  112. struct rpc_inode *rpci = RPC_I(inode);
  113. struct rpc_pipe_ops *ops;
  114. mutex_lock(&inode->i_mutex);
  115. ops = rpci->ops;
  116. if (ops != NULL) {
  117. LIST_HEAD(free_list);
  118. spin_lock(&inode->i_lock);
  119. rpci->nreaders = 0;
  120. list_splice_init(&rpci->in_upcall, &free_list);
  121. list_splice_init(&rpci->pipe, &free_list);
  122. rpci->pipelen = 0;
  123. rpci->ops = NULL;
  124. spin_unlock(&inode->i_lock);
  125. rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
  126. rpci->nwriters = 0;
  127. if (ops->release_pipe)
  128. ops->release_pipe(inode);
  129. cancel_delayed_work_sync(&rpci->queue_timeout);
  130. }
  131. rpc_inode_setowner(inode, NULL);
  132. mutex_unlock(&inode->i_mutex);
  133. }
  134. static struct inode *
  135. rpc_alloc_inode(struct super_block *sb)
  136. {
  137. struct rpc_inode *rpci;
  138. rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
  139. if (!rpci)
  140. return NULL;
  141. return &rpci->vfs_inode;
  142. }
  143. static void
  144. rpc_destroy_inode(struct inode *inode)
  145. {
  146. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  147. }
  148. static int
  149. rpc_pipe_open(struct inode *inode, struct file *filp)
  150. {
  151. struct rpc_inode *rpci = RPC_I(inode);
  152. int res = -ENXIO;
  153. mutex_lock(&inode->i_mutex);
  154. if (rpci->ops != NULL) {
  155. if (filp->f_mode & FMODE_READ)
  156. rpci->nreaders ++;
  157. if (filp->f_mode & FMODE_WRITE)
  158. rpci->nwriters ++;
  159. res = 0;
  160. }
  161. mutex_unlock(&inode->i_mutex);
  162. return res;
  163. }
  164. static int
  165. rpc_pipe_release(struct inode *inode, struct file *filp)
  166. {
  167. struct rpc_inode *rpci = RPC_I(inode);
  168. struct rpc_pipe_msg *msg;
  169. mutex_lock(&inode->i_mutex);
  170. if (rpci->ops == NULL)
  171. goto out;
  172. msg = (struct rpc_pipe_msg *)filp->private_data;
  173. if (msg != NULL) {
  174. spin_lock(&inode->i_lock);
  175. msg->errno = -EAGAIN;
  176. list_del(&msg->list);
  177. spin_unlock(&inode->i_lock);
  178. rpci->ops->destroy_msg(msg);
  179. }
  180. if (filp->f_mode & FMODE_WRITE)
  181. rpci->nwriters --;
  182. if (filp->f_mode & FMODE_READ) {
  183. rpci->nreaders --;
  184. if (rpci->nreaders == 0) {
  185. LIST_HEAD(free_list);
  186. spin_lock(&inode->i_lock);
  187. list_splice_init(&rpci->pipe, &free_list);
  188. rpci->pipelen = 0;
  189. spin_unlock(&inode->i_lock);
  190. rpc_purge_list(rpci, &free_list,
  191. rpci->ops->destroy_msg, -EAGAIN);
  192. }
  193. }
  194. if (rpci->ops->release_pipe)
  195. rpci->ops->release_pipe(inode);
  196. out:
  197. mutex_unlock(&inode->i_mutex);
  198. return 0;
  199. }
  200. static ssize_t
  201. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  202. {
  203. struct inode *inode = filp->f_path.dentry->d_inode;
  204. struct rpc_inode *rpci = RPC_I(inode);
  205. struct rpc_pipe_msg *msg;
  206. int res = 0;
  207. mutex_lock(&inode->i_mutex);
  208. if (rpci->ops == NULL) {
  209. res = -EPIPE;
  210. goto out_unlock;
  211. }
  212. msg = filp->private_data;
  213. if (msg == NULL) {
  214. spin_lock(&inode->i_lock);
  215. if (!list_empty(&rpci->pipe)) {
  216. msg = list_entry(rpci->pipe.next,
  217. struct rpc_pipe_msg,
  218. list);
  219. list_move(&msg->list, &rpci->in_upcall);
  220. rpci->pipelen -= msg->len;
  221. filp->private_data = msg;
  222. msg->copied = 0;
  223. }
  224. spin_unlock(&inode->i_lock);
  225. if (msg == NULL)
  226. goto out_unlock;
  227. }
  228. /* NOTE: it is up to the callback to update msg->copied */
  229. res = rpci->ops->upcall(filp, msg, buf, len);
  230. if (res < 0 || msg->len == msg->copied) {
  231. filp->private_data = NULL;
  232. spin_lock(&inode->i_lock);
  233. list_del(&msg->list);
  234. spin_unlock(&inode->i_lock);
  235. rpci->ops->destroy_msg(msg);
  236. }
  237. out_unlock:
  238. mutex_unlock(&inode->i_mutex);
  239. return res;
  240. }
  241. static ssize_t
  242. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  243. {
  244. struct inode *inode = filp->f_path.dentry->d_inode;
  245. struct rpc_inode *rpci = RPC_I(inode);
  246. int res;
  247. mutex_lock(&inode->i_mutex);
  248. res = -EPIPE;
  249. if (rpci->ops != NULL)
  250. res = rpci->ops->downcall(filp, buf, len);
  251. mutex_unlock(&inode->i_mutex);
  252. return res;
  253. }
  254. static unsigned int
  255. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  256. {
  257. struct rpc_inode *rpci;
  258. unsigned int mask = 0;
  259. rpci = RPC_I(filp->f_path.dentry->d_inode);
  260. poll_wait(filp, &rpci->waitq, wait);
  261. mask = POLLOUT | POLLWRNORM;
  262. if (rpci->ops == NULL)
  263. mask |= POLLERR | POLLHUP;
  264. if (filp->private_data || !list_empty(&rpci->pipe))
  265. mask |= POLLIN | POLLRDNORM;
  266. return mask;
  267. }
  268. static int
  269. rpc_pipe_ioctl(struct inode *ino, struct file *filp,
  270. unsigned int cmd, unsigned long arg)
  271. {
  272. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  273. int len;
  274. switch (cmd) {
  275. case FIONREAD:
  276. if (rpci->ops == NULL)
  277. return -EPIPE;
  278. len = rpci->pipelen;
  279. if (filp->private_data) {
  280. struct rpc_pipe_msg *msg;
  281. msg = (struct rpc_pipe_msg *)filp->private_data;
  282. len += msg->len - msg->copied;
  283. }
  284. return put_user(len, (int __user *)arg);
  285. default:
  286. return -EINVAL;
  287. }
  288. }
  289. static const struct file_operations rpc_pipe_fops = {
  290. .owner = THIS_MODULE,
  291. .llseek = no_llseek,
  292. .read = rpc_pipe_read,
  293. .write = rpc_pipe_write,
  294. .poll = rpc_pipe_poll,
  295. .ioctl = rpc_pipe_ioctl,
  296. .open = rpc_pipe_open,
  297. .release = rpc_pipe_release,
  298. };
  299. static int
  300. rpc_show_info(struct seq_file *m, void *v)
  301. {
  302. struct rpc_clnt *clnt = m->private;
  303. seq_printf(m, "RPC server: %s\n", clnt->cl_server);
  304. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
  305. clnt->cl_prog, clnt->cl_vers);
  306. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  307. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  308. seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
  309. return 0;
  310. }
  311. static int
  312. rpc_info_open(struct inode *inode, struct file *file)
  313. {
  314. struct rpc_clnt *clnt;
  315. int ret = single_open(file, rpc_show_info, NULL);
  316. if (!ret) {
  317. struct seq_file *m = file->private_data;
  318. mutex_lock(&inode->i_mutex);
  319. clnt = RPC_I(inode)->private;
  320. if (clnt) {
  321. kref_get(&clnt->cl_kref);
  322. m->private = clnt;
  323. } else {
  324. single_release(inode, file);
  325. ret = -EINVAL;
  326. }
  327. mutex_unlock(&inode->i_mutex);
  328. }
  329. return ret;
  330. }
  331. static int
  332. rpc_info_release(struct inode *inode, struct file *file)
  333. {
  334. struct seq_file *m = file->private_data;
  335. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  336. if (clnt)
  337. rpc_release_client(clnt);
  338. return single_release(inode, file);
  339. }
  340. static const struct file_operations rpc_info_operations = {
  341. .owner = THIS_MODULE,
  342. .open = rpc_info_open,
  343. .read = seq_read,
  344. .llseek = seq_lseek,
  345. .release = rpc_info_release,
  346. };
  347. /*
  348. * We have a single directory with 1 node in it.
  349. */
  350. enum {
  351. RPCAUTH_Root = 1,
  352. RPCAUTH_lockd,
  353. RPCAUTH_mount,
  354. RPCAUTH_nfs,
  355. RPCAUTH_portmap,
  356. RPCAUTH_statd,
  357. RPCAUTH_RootEOF
  358. };
  359. /*
  360. * Description of fs contents.
  361. */
  362. struct rpc_filelist {
  363. char *name;
  364. const struct file_operations *i_fop;
  365. int mode;
  366. };
  367. static struct rpc_filelist files[] = {
  368. [RPCAUTH_lockd] = {
  369. .name = "lockd",
  370. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  371. },
  372. [RPCAUTH_mount] = {
  373. .name = "mount",
  374. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  375. },
  376. [RPCAUTH_nfs] = {
  377. .name = "nfs",
  378. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  379. },
  380. [RPCAUTH_portmap] = {
  381. .name = "portmap",
  382. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  383. },
  384. [RPCAUTH_statd] = {
  385. .name = "statd",
  386. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  387. },
  388. };
  389. enum {
  390. RPCAUTH_info = 2,
  391. RPCAUTH_EOF
  392. };
  393. static struct rpc_filelist authfiles[] = {
  394. [RPCAUTH_info] = {
  395. .name = "info",
  396. .i_fop = &rpc_info_operations,
  397. .mode = S_IFREG | S_IRUSR,
  398. },
  399. };
  400. struct vfsmount *rpc_get_mount(void)
  401. {
  402. int err;
  403. err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
  404. if (err != 0)
  405. return ERR_PTR(err);
  406. return rpc_mount;
  407. }
  408. void rpc_put_mount(void)
  409. {
  410. simple_release_fs(&rpc_mount, &rpc_mount_count);
  411. }
  412. static int rpc_delete_dentry(struct dentry *dentry)
  413. {
  414. return 1;
  415. }
  416. static struct dentry_operations rpc_dentry_operations = {
  417. .d_delete = rpc_delete_dentry,
  418. };
  419. static int
  420. rpc_lookup_parent(char *path, struct nameidata *nd)
  421. {
  422. struct vfsmount *mnt;
  423. if (path[0] == '\0')
  424. return -ENOENT;
  425. mnt = rpc_get_mount();
  426. if (IS_ERR(mnt)) {
  427. printk(KERN_WARNING "%s: %s failed to mount "
  428. "pseudofilesystem \n", __FILE__, __FUNCTION__);
  429. return PTR_ERR(mnt);
  430. }
  431. if (vfs_path_lookup(mnt->mnt_root, mnt, path, LOOKUP_PARENT, nd)) {
  432. printk(KERN_WARNING "%s: %s failed to find path %s\n",
  433. __FILE__, __FUNCTION__, path);
  434. rpc_put_mount();
  435. return -ENOENT;
  436. }
  437. return 0;
  438. }
  439. static void
  440. rpc_release_path(struct nameidata *nd)
  441. {
  442. path_release(nd);
  443. rpc_put_mount();
  444. }
  445. static struct inode *
  446. rpc_get_inode(struct super_block *sb, int mode)
  447. {
  448. struct inode *inode = new_inode(sb);
  449. if (!inode)
  450. return NULL;
  451. inode->i_mode = mode;
  452. inode->i_uid = inode->i_gid = 0;
  453. inode->i_blocks = 0;
  454. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  455. switch(mode & S_IFMT) {
  456. case S_IFDIR:
  457. inode->i_fop = &simple_dir_operations;
  458. inode->i_op = &simple_dir_inode_operations;
  459. inc_nlink(inode);
  460. default:
  461. break;
  462. }
  463. return inode;
  464. }
  465. /*
  466. * FIXME: This probably has races.
  467. */
  468. static void rpc_depopulate(struct dentry *parent,
  469. unsigned long start, unsigned long eof)
  470. {
  471. struct inode *dir = parent->d_inode;
  472. struct list_head *pos, *next;
  473. struct dentry *dentry, *dvec[10];
  474. int n = 0;
  475. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  476. repeat:
  477. spin_lock(&dcache_lock);
  478. list_for_each_safe(pos, next, &parent->d_subdirs) {
  479. dentry = list_entry(pos, struct dentry, d_u.d_child);
  480. if (!dentry->d_inode ||
  481. dentry->d_inode->i_ino < start ||
  482. dentry->d_inode->i_ino >= eof)
  483. continue;
  484. spin_lock(&dentry->d_lock);
  485. if (!d_unhashed(dentry)) {
  486. dget_locked(dentry);
  487. __d_drop(dentry);
  488. spin_unlock(&dentry->d_lock);
  489. dvec[n++] = dentry;
  490. if (n == ARRAY_SIZE(dvec))
  491. break;
  492. } else
  493. spin_unlock(&dentry->d_lock);
  494. }
  495. spin_unlock(&dcache_lock);
  496. if (n) {
  497. do {
  498. dentry = dvec[--n];
  499. if (S_ISREG(dentry->d_inode->i_mode))
  500. simple_unlink(dir, dentry);
  501. else if (S_ISDIR(dentry->d_inode->i_mode))
  502. simple_rmdir(dir, dentry);
  503. d_delete(dentry);
  504. dput(dentry);
  505. } while (n);
  506. goto repeat;
  507. }
  508. mutex_unlock(&dir->i_mutex);
  509. }
  510. static int
  511. rpc_populate(struct dentry *parent,
  512. struct rpc_filelist *files,
  513. int start, int eof)
  514. {
  515. struct inode *inode, *dir = parent->d_inode;
  516. void *private = RPC_I(dir)->private;
  517. struct dentry *dentry;
  518. int mode, i;
  519. mutex_lock(&dir->i_mutex);
  520. for (i = start; i < eof; i++) {
  521. dentry = d_alloc_name(parent, files[i].name);
  522. if (!dentry)
  523. goto out_bad;
  524. dentry->d_op = &rpc_dentry_operations;
  525. mode = files[i].mode;
  526. inode = rpc_get_inode(dir->i_sb, mode);
  527. if (!inode) {
  528. dput(dentry);
  529. goto out_bad;
  530. }
  531. inode->i_ino = i;
  532. if (files[i].i_fop)
  533. inode->i_fop = files[i].i_fop;
  534. if (private)
  535. rpc_inode_setowner(inode, private);
  536. if (S_ISDIR(mode))
  537. inc_nlink(dir);
  538. d_add(dentry, inode);
  539. fsnotify_create(dir, dentry);
  540. }
  541. mutex_unlock(&dir->i_mutex);
  542. return 0;
  543. out_bad:
  544. mutex_unlock(&dir->i_mutex);
  545. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  546. __FILE__, __FUNCTION__, parent->d_name.name);
  547. return -ENOMEM;
  548. }
  549. static int
  550. __rpc_mkdir(struct inode *dir, struct dentry *dentry)
  551. {
  552. struct inode *inode;
  553. inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  554. if (!inode)
  555. goto out_err;
  556. inode->i_ino = iunique(dir->i_sb, 100);
  557. d_instantiate(dentry, inode);
  558. inc_nlink(dir);
  559. fsnotify_mkdir(dir, dentry);
  560. return 0;
  561. out_err:
  562. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  563. __FILE__, __FUNCTION__, dentry->d_name.name);
  564. return -ENOMEM;
  565. }
  566. static int
  567. __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  568. {
  569. int error;
  570. error = simple_rmdir(dir, dentry);
  571. if (!error)
  572. d_delete(dentry);
  573. return error;
  574. }
  575. static struct dentry *
  576. rpc_lookup_create(struct dentry *parent, const char *name, int len, int exclusive)
  577. {
  578. struct inode *dir = parent->d_inode;
  579. struct dentry *dentry;
  580. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  581. dentry = lookup_one_len(name, parent, len);
  582. if (IS_ERR(dentry))
  583. goto out_err;
  584. if (!dentry->d_inode)
  585. dentry->d_op = &rpc_dentry_operations;
  586. else if (exclusive) {
  587. dput(dentry);
  588. dentry = ERR_PTR(-EEXIST);
  589. goto out_err;
  590. }
  591. return dentry;
  592. out_err:
  593. mutex_unlock(&dir->i_mutex);
  594. return dentry;
  595. }
  596. static struct dentry *
  597. rpc_lookup_negative(char *path, struct nameidata *nd)
  598. {
  599. struct dentry *dentry;
  600. int error;
  601. if ((error = rpc_lookup_parent(path, nd)) != 0)
  602. return ERR_PTR(error);
  603. dentry = rpc_lookup_create(nd->path.dentry, nd->last.name, nd->last.len,
  604. 1);
  605. if (IS_ERR(dentry))
  606. rpc_release_path(nd);
  607. return dentry;
  608. }
  609. /**
  610. * rpc_mkdir - Create a new directory in rpc_pipefs
  611. * @path: path from the rpc_pipefs root to the new directory
  612. * @rpc_client: rpc client to associate with this directory
  613. *
  614. * This creates a directory at the given @path associated with
  615. * @rpc_clnt, which will contain a file named "info" with some basic
  616. * information about the client, together with any "pipes" that may
  617. * later be created using rpc_mkpipe().
  618. */
  619. struct dentry *
  620. rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
  621. {
  622. struct nameidata nd;
  623. struct dentry *dentry;
  624. struct inode *dir;
  625. int error;
  626. dentry = rpc_lookup_negative(path, &nd);
  627. if (IS_ERR(dentry))
  628. return dentry;
  629. dir = nd.path.dentry->d_inode;
  630. if ((error = __rpc_mkdir(dir, dentry)) != 0)
  631. goto err_dput;
  632. RPC_I(dentry->d_inode)->private = rpc_client;
  633. error = rpc_populate(dentry, authfiles,
  634. RPCAUTH_info, RPCAUTH_EOF);
  635. if (error)
  636. goto err_depopulate;
  637. dget(dentry);
  638. out:
  639. mutex_unlock(&dir->i_mutex);
  640. rpc_release_path(&nd);
  641. return dentry;
  642. err_depopulate:
  643. rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
  644. __rpc_rmdir(dir, dentry);
  645. err_dput:
  646. dput(dentry);
  647. printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
  648. __FILE__, __FUNCTION__, path, error);
  649. dentry = ERR_PTR(error);
  650. goto out;
  651. }
  652. /**
  653. * rpc_rmdir - Remove a directory created with rpc_mkdir()
  654. * @dentry: directory to remove
  655. */
  656. int
  657. rpc_rmdir(struct dentry *dentry)
  658. {
  659. struct dentry *parent;
  660. struct inode *dir;
  661. int error;
  662. parent = dget_parent(dentry);
  663. dir = parent->d_inode;
  664. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  665. rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
  666. error = __rpc_rmdir(dir, dentry);
  667. dput(dentry);
  668. mutex_unlock(&dir->i_mutex);
  669. dput(parent);
  670. return error;
  671. }
  672. /**
  673. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  674. * @parent: dentry of directory to create new "pipe" in
  675. * @name: name of pipe
  676. * @private: private data to associate with the pipe, for the caller's use
  677. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  678. * release_pipe, and destroy_msg.
  679. * @flags: rpc_inode flags
  680. *
  681. * Data is made available for userspace to read by calls to
  682. * rpc_queue_upcall(). The actual reads will result in calls to
  683. * @ops->upcall, which will be called with the file pointer,
  684. * message, and userspace buffer to copy to.
  685. *
  686. * Writes can come at any time, and do not necessarily have to be
  687. * responses to upcalls. They will result in calls to @msg->downcall.
  688. *
  689. * The @private argument passed here will be available to all these methods
  690. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  691. */
  692. struct dentry *
  693. rpc_mkpipe(struct dentry *parent, const char *name, void *private, struct rpc_pipe_ops *ops, int flags)
  694. {
  695. struct dentry *dentry;
  696. struct inode *dir, *inode;
  697. struct rpc_inode *rpci;
  698. dentry = rpc_lookup_create(parent, name, strlen(name), 0);
  699. if (IS_ERR(dentry))
  700. return dentry;
  701. dir = parent->d_inode;
  702. if (dentry->d_inode) {
  703. rpci = RPC_I(dentry->d_inode);
  704. if (rpci->private != private ||
  705. rpci->ops != ops ||
  706. rpci->flags != flags) {
  707. dput (dentry);
  708. dentry = ERR_PTR(-EBUSY);
  709. }
  710. rpci->nkern_readwriters++;
  711. goto out;
  712. }
  713. inode = rpc_get_inode(dir->i_sb, S_IFIFO | S_IRUSR | S_IWUSR);
  714. if (!inode)
  715. goto err_dput;
  716. inode->i_ino = iunique(dir->i_sb, 100);
  717. inode->i_fop = &rpc_pipe_fops;
  718. d_instantiate(dentry, inode);
  719. rpci = RPC_I(inode);
  720. rpci->private = private;
  721. rpci->flags = flags;
  722. rpci->ops = ops;
  723. rpci->nkern_readwriters = 1;
  724. fsnotify_create(dir, dentry);
  725. dget(dentry);
  726. out:
  727. mutex_unlock(&dir->i_mutex);
  728. return dentry;
  729. err_dput:
  730. dput(dentry);
  731. dentry = ERR_PTR(-ENOMEM);
  732. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  733. __FILE__, __FUNCTION__, parent->d_name.name, name,
  734. -ENOMEM);
  735. goto out;
  736. }
  737. EXPORT_SYMBOL(rpc_mkpipe);
  738. /**
  739. * rpc_unlink - remove a pipe
  740. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  741. *
  742. * After this call, lookups will no longer find the pipe, and any
  743. * attempts to read or write using preexisting opens of the pipe will
  744. * return -EPIPE.
  745. */
  746. int
  747. rpc_unlink(struct dentry *dentry)
  748. {
  749. struct dentry *parent;
  750. struct inode *dir;
  751. int error = 0;
  752. parent = dget_parent(dentry);
  753. dir = parent->d_inode;
  754. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  755. if (--RPC_I(dentry->d_inode)->nkern_readwriters == 0) {
  756. rpc_close_pipes(dentry->d_inode);
  757. error = simple_unlink(dir, dentry);
  758. if (!error)
  759. d_delete(dentry);
  760. }
  761. dput(dentry);
  762. mutex_unlock(&dir->i_mutex);
  763. dput(parent);
  764. return error;
  765. }
  766. EXPORT_SYMBOL(rpc_unlink);
  767. /*
  768. * populate the filesystem
  769. */
  770. static struct super_operations s_ops = {
  771. .alloc_inode = rpc_alloc_inode,
  772. .destroy_inode = rpc_destroy_inode,
  773. .statfs = simple_statfs,
  774. };
  775. #define RPCAUTH_GSSMAGIC 0x67596969
  776. static int
  777. rpc_fill_super(struct super_block *sb, void *data, int silent)
  778. {
  779. struct inode *inode;
  780. struct dentry *root;
  781. sb->s_blocksize = PAGE_CACHE_SIZE;
  782. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  783. sb->s_magic = RPCAUTH_GSSMAGIC;
  784. sb->s_op = &s_ops;
  785. sb->s_time_gran = 1;
  786. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  787. if (!inode)
  788. return -ENOMEM;
  789. root = d_alloc_root(inode);
  790. if (!root) {
  791. iput(inode);
  792. return -ENOMEM;
  793. }
  794. if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
  795. goto out;
  796. sb->s_root = root;
  797. return 0;
  798. out:
  799. d_genocide(root);
  800. dput(root);
  801. return -ENOMEM;
  802. }
  803. static int
  804. rpc_get_sb(struct file_system_type *fs_type,
  805. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  806. {
  807. return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
  808. }
  809. static struct file_system_type rpc_pipe_fs_type = {
  810. .owner = THIS_MODULE,
  811. .name = "rpc_pipefs",
  812. .get_sb = rpc_get_sb,
  813. .kill_sb = kill_litter_super,
  814. };
  815. static void
  816. init_once(struct kmem_cache * cachep, void *foo)
  817. {
  818. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  819. inode_init_once(&rpci->vfs_inode);
  820. rpci->private = NULL;
  821. rpci->nreaders = 0;
  822. rpci->nwriters = 0;
  823. INIT_LIST_HEAD(&rpci->in_upcall);
  824. INIT_LIST_HEAD(&rpci->in_downcall);
  825. INIT_LIST_HEAD(&rpci->pipe);
  826. rpci->pipelen = 0;
  827. init_waitqueue_head(&rpci->waitq);
  828. INIT_DELAYED_WORK(&rpci->queue_timeout,
  829. rpc_timeout_upcall_queue);
  830. rpci->ops = NULL;
  831. }
  832. int register_rpc_pipefs(void)
  833. {
  834. int err;
  835. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  836. sizeof(struct rpc_inode),
  837. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  838. SLAB_MEM_SPREAD),
  839. init_once);
  840. if (!rpc_inode_cachep)
  841. return -ENOMEM;
  842. err = register_filesystem(&rpc_pipe_fs_type);
  843. if (err) {
  844. kmem_cache_destroy(rpc_inode_cachep);
  845. return err;
  846. }
  847. return 0;
  848. }
  849. void unregister_rpc_pipefs(void)
  850. {
  851. kmem_cache_destroy(rpc_inode_cachep);
  852. unregister_filesystem(&rpc_pipe_fs_type);
  853. }