rpc_pipe.c 22 KB

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