rpc_pipe.c 21 KB

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