rpc_pipe.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. #include <linux/sunrpc/cache.h>
  28. #include <linux/nsproxy.h>
  29. #include <linux/notifier.h>
  30. #include "netns.h"
  31. #include "sunrpc.h"
  32. #define RPCDBG_FACILITY RPCDBG_DEBUG
  33. #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
  34. static struct vfsmount *rpc_mnt __read_mostly;
  35. static int rpc_mount_count;
  36. static struct file_system_type rpc_pipe_fs_type;
  37. static struct kmem_cache *rpc_inode_cachep __read_mostly;
  38. #define RPC_UPCALL_TIMEOUT (30*HZ)
  39. static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
  40. int rpc_pipefs_notifier_register(struct notifier_block *nb)
  41. {
  42. return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
  43. }
  44. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
  45. void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
  46. {
  47. blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
  48. }
  49. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
  50. static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
  51. void (*destroy_msg)(struct rpc_pipe_msg *), int err)
  52. {
  53. struct rpc_pipe_msg *msg;
  54. if (list_empty(head))
  55. return;
  56. do {
  57. msg = list_entry(head->next, struct rpc_pipe_msg, list);
  58. list_del_init(&msg->list);
  59. msg->errno = err;
  60. destroy_msg(msg);
  61. } while (!list_empty(head));
  62. wake_up(&pipe->waitq);
  63. }
  64. static void
  65. rpc_timeout_upcall_queue(struct work_struct *work)
  66. {
  67. LIST_HEAD(free_list);
  68. struct rpc_pipe *pipe =
  69. container_of(work, struct rpc_pipe, queue_timeout.work);
  70. void (*destroy_msg)(struct rpc_pipe_msg *);
  71. spin_lock(&pipe->lock);
  72. if (pipe->ops == NULL) {
  73. spin_unlock(&pipe->lock);
  74. return;
  75. }
  76. destroy_msg = pipe->ops->destroy_msg;
  77. if (pipe->nreaders == 0) {
  78. list_splice_init(&pipe->pipe, &free_list);
  79. pipe->pipelen = 0;
  80. }
  81. spin_unlock(&pipe->lock);
  82. rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
  83. }
  84. ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  85. char __user *dst, size_t buflen)
  86. {
  87. char *data = (char *)msg->data + msg->copied;
  88. size_t mlen = min(msg->len - msg->copied, buflen);
  89. unsigned long left;
  90. left = copy_to_user(dst, data, mlen);
  91. if (left == mlen) {
  92. msg->errno = -EFAULT;
  93. return -EFAULT;
  94. }
  95. mlen -= left;
  96. msg->copied += mlen;
  97. msg->errno = 0;
  98. return mlen;
  99. }
  100. EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
  101. /**
  102. * rpc_queue_upcall - queue an upcall message to userspace
  103. * @inode: inode of upcall pipe on which to queue given message
  104. * @msg: message to queue
  105. *
  106. * Call with an @inode created by rpc_mkpipe() to queue an upcall.
  107. * A userspace process may then later read the upcall by performing a
  108. * read on an open file for this inode. It is up to the caller to
  109. * initialize the fields of @msg (other than @msg->list) appropriately.
  110. */
  111. int
  112. rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
  113. {
  114. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  115. int res = -EPIPE;
  116. spin_lock(&pipe->lock);
  117. if (pipe->ops == NULL)
  118. goto out;
  119. if (pipe->nreaders) {
  120. list_add_tail(&msg->list, &pipe->pipe);
  121. pipe->pipelen += msg->len;
  122. res = 0;
  123. } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  124. if (list_empty(&pipe->pipe))
  125. queue_delayed_work(rpciod_workqueue,
  126. &pipe->queue_timeout,
  127. RPC_UPCALL_TIMEOUT);
  128. list_add_tail(&msg->list, &pipe->pipe);
  129. pipe->pipelen += msg->len;
  130. res = 0;
  131. }
  132. out:
  133. spin_unlock(&pipe->lock);
  134. wake_up(&pipe->waitq);
  135. return res;
  136. }
  137. EXPORT_SYMBOL_GPL(rpc_queue_upcall);
  138. static inline void
  139. rpc_inode_setowner(struct inode *inode, void *private)
  140. {
  141. RPC_I(inode)->private = private;
  142. }
  143. static void
  144. rpc_close_pipes(struct inode *inode)
  145. {
  146. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  147. const struct rpc_pipe_ops *ops;
  148. int need_release;
  149. mutex_lock(&inode->i_mutex);
  150. ops = pipe->ops;
  151. if (ops != NULL) {
  152. LIST_HEAD(free_list);
  153. spin_lock(&pipe->lock);
  154. need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
  155. pipe->nreaders = 0;
  156. list_splice_init(&pipe->in_upcall, &free_list);
  157. list_splice_init(&pipe->pipe, &free_list);
  158. pipe->pipelen = 0;
  159. pipe->ops = NULL;
  160. spin_unlock(&pipe->lock);
  161. rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
  162. pipe->nwriters = 0;
  163. if (need_release && ops->release_pipe)
  164. ops->release_pipe(inode);
  165. cancel_delayed_work_sync(&pipe->queue_timeout);
  166. }
  167. rpc_inode_setowner(inode, NULL);
  168. mutex_unlock(&inode->i_mutex);
  169. }
  170. static struct inode *
  171. rpc_alloc_inode(struct super_block *sb)
  172. {
  173. struct rpc_inode *rpci;
  174. rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
  175. if (!rpci)
  176. return NULL;
  177. return &rpci->vfs_inode;
  178. }
  179. static void
  180. rpc_i_callback(struct rcu_head *head)
  181. {
  182. struct inode *inode = container_of(head, struct inode, i_rcu);
  183. kfree(RPC_I(inode)->pipe);
  184. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  185. }
  186. static void
  187. rpc_destroy_inode(struct inode *inode)
  188. {
  189. call_rcu(&inode->i_rcu, rpc_i_callback);
  190. }
  191. static int
  192. rpc_pipe_open(struct inode *inode, struct file *filp)
  193. {
  194. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  195. int first_open;
  196. int res = -ENXIO;
  197. mutex_lock(&inode->i_mutex);
  198. if (pipe->ops == NULL)
  199. goto out;
  200. first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
  201. if (first_open && pipe->ops->open_pipe) {
  202. res = pipe->ops->open_pipe(inode);
  203. if (res)
  204. goto out;
  205. }
  206. if (filp->f_mode & FMODE_READ)
  207. pipe->nreaders++;
  208. if (filp->f_mode & FMODE_WRITE)
  209. pipe->nwriters++;
  210. res = 0;
  211. out:
  212. mutex_unlock(&inode->i_mutex);
  213. return res;
  214. }
  215. static int
  216. rpc_pipe_release(struct inode *inode, struct file *filp)
  217. {
  218. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  219. struct rpc_pipe_msg *msg;
  220. int last_close;
  221. mutex_lock(&inode->i_mutex);
  222. if (pipe->ops == NULL)
  223. goto out;
  224. msg = filp->private_data;
  225. if (msg != NULL) {
  226. spin_lock(&pipe->lock);
  227. msg->errno = -EAGAIN;
  228. list_del_init(&msg->list);
  229. spin_unlock(&pipe->lock);
  230. pipe->ops->destroy_msg(msg);
  231. }
  232. if (filp->f_mode & FMODE_WRITE)
  233. pipe->nwriters --;
  234. if (filp->f_mode & FMODE_READ) {
  235. pipe->nreaders --;
  236. if (pipe->nreaders == 0) {
  237. LIST_HEAD(free_list);
  238. spin_lock(&pipe->lock);
  239. list_splice_init(&pipe->pipe, &free_list);
  240. pipe->pipelen = 0;
  241. spin_unlock(&pipe->lock);
  242. rpc_purge_list(pipe, &free_list,
  243. pipe->ops->destroy_msg, -EAGAIN);
  244. }
  245. }
  246. last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
  247. if (last_close && pipe->ops->release_pipe)
  248. pipe->ops->release_pipe(inode);
  249. out:
  250. mutex_unlock(&inode->i_mutex);
  251. return 0;
  252. }
  253. static ssize_t
  254. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  255. {
  256. struct inode *inode = filp->f_path.dentry->d_inode;
  257. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  258. struct rpc_pipe_msg *msg;
  259. int res = 0;
  260. mutex_lock(&inode->i_mutex);
  261. if (pipe->ops == NULL) {
  262. res = -EPIPE;
  263. goto out_unlock;
  264. }
  265. msg = filp->private_data;
  266. if (msg == NULL) {
  267. spin_lock(&pipe->lock);
  268. if (!list_empty(&pipe->pipe)) {
  269. msg = list_entry(pipe->pipe.next,
  270. struct rpc_pipe_msg,
  271. list);
  272. list_move(&msg->list, &pipe->in_upcall);
  273. pipe->pipelen -= msg->len;
  274. filp->private_data = msg;
  275. msg->copied = 0;
  276. }
  277. spin_unlock(&pipe->lock);
  278. if (msg == NULL)
  279. goto out_unlock;
  280. }
  281. /* NOTE: it is up to the callback to update msg->copied */
  282. res = pipe->ops->upcall(filp, msg, buf, len);
  283. if (res < 0 || msg->len == msg->copied) {
  284. filp->private_data = NULL;
  285. spin_lock(&pipe->lock);
  286. list_del_init(&msg->list);
  287. spin_unlock(&pipe->lock);
  288. pipe->ops->destroy_msg(msg);
  289. }
  290. out_unlock:
  291. mutex_unlock(&inode->i_mutex);
  292. return res;
  293. }
  294. static ssize_t
  295. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  296. {
  297. struct inode *inode = filp->f_path.dentry->d_inode;
  298. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  299. int res;
  300. mutex_lock(&inode->i_mutex);
  301. res = -EPIPE;
  302. if (pipe->ops != NULL)
  303. res = pipe->ops->downcall(filp, buf, len);
  304. mutex_unlock(&inode->i_mutex);
  305. return res;
  306. }
  307. static unsigned int
  308. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  309. {
  310. struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
  311. unsigned int mask = 0;
  312. poll_wait(filp, &pipe->waitq, wait);
  313. mask = POLLOUT | POLLWRNORM;
  314. if (pipe->ops == NULL)
  315. mask |= POLLERR | POLLHUP;
  316. if (filp->private_data || !list_empty(&pipe->pipe))
  317. mask |= POLLIN | POLLRDNORM;
  318. return mask;
  319. }
  320. static long
  321. rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  322. {
  323. struct inode *inode = filp->f_path.dentry->d_inode;
  324. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  325. int len;
  326. switch (cmd) {
  327. case FIONREAD:
  328. spin_lock(&pipe->lock);
  329. if (pipe->ops == NULL) {
  330. spin_unlock(&pipe->lock);
  331. return -EPIPE;
  332. }
  333. len = pipe->pipelen;
  334. if (filp->private_data) {
  335. struct rpc_pipe_msg *msg;
  336. msg = filp->private_data;
  337. len += msg->len - msg->copied;
  338. }
  339. spin_unlock(&pipe->lock);
  340. return put_user(len, (int __user *)arg);
  341. default:
  342. return -EINVAL;
  343. }
  344. }
  345. static const struct file_operations rpc_pipe_fops = {
  346. .owner = THIS_MODULE,
  347. .llseek = no_llseek,
  348. .read = rpc_pipe_read,
  349. .write = rpc_pipe_write,
  350. .poll = rpc_pipe_poll,
  351. .unlocked_ioctl = rpc_pipe_ioctl,
  352. .open = rpc_pipe_open,
  353. .release = rpc_pipe_release,
  354. };
  355. static int
  356. rpc_show_info(struct seq_file *m, void *v)
  357. {
  358. struct rpc_clnt *clnt = m->private;
  359. seq_printf(m, "RPC server: %s\n", clnt->cl_server);
  360. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
  361. clnt->cl_prog, clnt->cl_vers);
  362. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  363. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  364. seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
  365. return 0;
  366. }
  367. static int
  368. rpc_info_open(struct inode *inode, struct file *file)
  369. {
  370. struct rpc_clnt *clnt = NULL;
  371. int ret = single_open(file, rpc_show_info, NULL);
  372. if (!ret) {
  373. struct seq_file *m = file->private_data;
  374. spin_lock(&file->f_path.dentry->d_lock);
  375. if (!d_unhashed(file->f_path.dentry))
  376. clnt = RPC_I(inode)->private;
  377. if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
  378. spin_unlock(&file->f_path.dentry->d_lock);
  379. m->private = clnt;
  380. } else {
  381. spin_unlock(&file->f_path.dentry->d_lock);
  382. single_release(inode, file);
  383. ret = -EINVAL;
  384. }
  385. }
  386. return ret;
  387. }
  388. static int
  389. rpc_info_release(struct inode *inode, struct file *file)
  390. {
  391. struct seq_file *m = file->private_data;
  392. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  393. if (clnt)
  394. rpc_release_client(clnt);
  395. return single_release(inode, file);
  396. }
  397. static const struct file_operations rpc_info_operations = {
  398. .owner = THIS_MODULE,
  399. .open = rpc_info_open,
  400. .read = seq_read,
  401. .llseek = seq_lseek,
  402. .release = rpc_info_release,
  403. };
  404. /*
  405. * Description of fs contents.
  406. */
  407. struct rpc_filelist {
  408. const char *name;
  409. const struct file_operations *i_fop;
  410. umode_t mode;
  411. };
  412. struct vfsmount *rpc_get_mount(void)
  413. {
  414. int err;
  415. err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
  416. if (err != 0)
  417. return ERR_PTR(err);
  418. return rpc_mnt;
  419. }
  420. EXPORT_SYMBOL_GPL(rpc_get_mount);
  421. void rpc_put_mount(void)
  422. {
  423. simple_release_fs(&rpc_mnt, &rpc_mount_count);
  424. }
  425. EXPORT_SYMBOL_GPL(rpc_put_mount);
  426. static int rpc_delete_dentry(const struct dentry *dentry)
  427. {
  428. return 1;
  429. }
  430. static const struct dentry_operations rpc_dentry_operations = {
  431. .d_delete = rpc_delete_dentry,
  432. };
  433. static struct inode *
  434. rpc_get_inode(struct super_block *sb, umode_t mode)
  435. {
  436. struct inode *inode = new_inode(sb);
  437. if (!inode)
  438. return NULL;
  439. inode->i_ino = get_next_ino();
  440. inode->i_mode = mode;
  441. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  442. switch (mode & S_IFMT) {
  443. case S_IFDIR:
  444. inode->i_fop = &simple_dir_operations;
  445. inode->i_op = &simple_dir_inode_operations;
  446. inc_nlink(inode);
  447. default:
  448. break;
  449. }
  450. return inode;
  451. }
  452. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  453. umode_t mode,
  454. const struct file_operations *i_fop,
  455. void *private)
  456. {
  457. struct inode *inode;
  458. d_drop(dentry);
  459. inode = rpc_get_inode(dir->i_sb, mode);
  460. if (!inode)
  461. goto out_err;
  462. inode->i_ino = iunique(dir->i_sb, 100);
  463. if (i_fop)
  464. inode->i_fop = i_fop;
  465. if (private)
  466. rpc_inode_setowner(inode, private);
  467. d_add(dentry, inode);
  468. return 0;
  469. out_err:
  470. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  471. __FILE__, __func__, dentry->d_name.name);
  472. dput(dentry);
  473. return -ENOMEM;
  474. }
  475. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  476. umode_t mode,
  477. const struct file_operations *i_fop,
  478. void *private)
  479. {
  480. int err;
  481. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  482. if (err)
  483. return err;
  484. fsnotify_create(dir, dentry);
  485. return 0;
  486. }
  487. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  488. umode_t mode,
  489. const struct file_operations *i_fop,
  490. void *private)
  491. {
  492. int err;
  493. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  494. if (err)
  495. return err;
  496. inc_nlink(dir);
  497. fsnotify_mkdir(dir, dentry);
  498. return 0;
  499. }
  500. static void
  501. init_pipe(struct rpc_pipe *pipe)
  502. {
  503. pipe->nreaders = 0;
  504. pipe->nwriters = 0;
  505. INIT_LIST_HEAD(&pipe->in_upcall);
  506. INIT_LIST_HEAD(&pipe->in_downcall);
  507. INIT_LIST_HEAD(&pipe->pipe);
  508. pipe->pipelen = 0;
  509. init_waitqueue_head(&pipe->waitq);
  510. INIT_DELAYED_WORK(&pipe->queue_timeout,
  511. rpc_timeout_upcall_queue);
  512. pipe->ops = NULL;
  513. spin_lock_init(&pipe->lock);
  514. }
  515. static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
  516. umode_t mode,
  517. const struct file_operations *i_fop,
  518. void *private,
  519. const struct rpc_pipe_ops *ops,
  520. int flags)
  521. {
  522. struct rpc_pipe *pipe;
  523. struct rpc_inode *rpci;
  524. int err;
  525. pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
  526. if (!pipe)
  527. return -ENOMEM;
  528. init_pipe(pipe);
  529. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  530. if (err) {
  531. kfree(pipe);
  532. return err;
  533. }
  534. rpci = RPC_I(dentry->d_inode);
  535. rpci->private = private;
  536. rpci->pipe = pipe;
  537. rpci->pipe->flags = flags;
  538. rpci->pipe->ops = ops;
  539. fsnotify_create(dir, dentry);
  540. return 0;
  541. }
  542. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  543. {
  544. int ret;
  545. dget(dentry);
  546. ret = simple_rmdir(dir, dentry);
  547. d_delete(dentry);
  548. dput(dentry);
  549. return ret;
  550. }
  551. static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
  552. {
  553. int ret;
  554. dget(dentry);
  555. ret = simple_unlink(dir, dentry);
  556. d_delete(dentry);
  557. dput(dentry);
  558. return ret;
  559. }
  560. static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
  561. {
  562. struct inode *inode = dentry->d_inode;
  563. rpc_close_pipes(inode);
  564. return __rpc_unlink(dir, dentry);
  565. }
  566. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  567. struct qstr *name)
  568. {
  569. struct dentry *dentry;
  570. dentry = d_lookup(parent, name);
  571. if (!dentry) {
  572. dentry = d_alloc(parent, name);
  573. if (!dentry)
  574. return ERR_PTR(-ENOMEM);
  575. }
  576. if (dentry->d_inode == NULL) {
  577. d_set_d_op(dentry, &rpc_dentry_operations);
  578. return dentry;
  579. }
  580. dput(dentry);
  581. return ERR_PTR(-EEXIST);
  582. }
  583. /*
  584. * FIXME: This probably has races.
  585. */
  586. static void __rpc_depopulate(struct dentry *parent,
  587. const struct rpc_filelist *files,
  588. int start, int eof)
  589. {
  590. struct inode *dir = parent->d_inode;
  591. struct dentry *dentry;
  592. struct qstr name;
  593. int i;
  594. for (i = start; i < eof; i++) {
  595. name.name = files[i].name;
  596. name.len = strlen(files[i].name);
  597. name.hash = full_name_hash(name.name, name.len);
  598. dentry = d_lookup(parent, &name);
  599. if (dentry == NULL)
  600. continue;
  601. if (dentry->d_inode == NULL)
  602. goto next;
  603. switch (dentry->d_inode->i_mode & S_IFMT) {
  604. default:
  605. BUG();
  606. case S_IFREG:
  607. __rpc_unlink(dir, dentry);
  608. break;
  609. case S_IFDIR:
  610. __rpc_rmdir(dir, dentry);
  611. }
  612. next:
  613. dput(dentry);
  614. }
  615. }
  616. static void rpc_depopulate(struct dentry *parent,
  617. const struct rpc_filelist *files,
  618. int start, int eof)
  619. {
  620. struct inode *dir = parent->d_inode;
  621. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  622. __rpc_depopulate(parent, files, start, eof);
  623. mutex_unlock(&dir->i_mutex);
  624. }
  625. static int rpc_populate(struct dentry *parent,
  626. const struct rpc_filelist *files,
  627. int start, int eof,
  628. void *private)
  629. {
  630. struct inode *dir = parent->d_inode;
  631. struct dentry *dentry;
  632. int i, err;
  633. mutex_lock(&dir->i_mutex);
  634. for (i = start; i < eof; i++) {
  635. struct qstr q;
  636. q.name = files[i].name;
  637. q.len = strlen(files[i].name);
  638. q.hash = full_name_hash(q.name, q.len);
  639. dentry = __rpc_lookup_create_exclusive(parent, &q);
  640. err = PTR_ERR(dentry);
  641. if (IS_ERR(dentry))
  642. goto out_bad;
  643. switch (files[i].mode & S_IFMT) {
  644. default:
  645. BUG();
  646. case S_IFREG:
  647. err = __rpc_create(dir, dentry,
  648. files[i].mode,
  649. files[i].i_fop,
  650. private);
  651. break;
  652. case S_IFDIR:
  653. err = __rpc_mkdir(dir, dentry,
  654. files[i].mode,
  655. NULL,
  656. private);
  657. }
  658. if (err != 0)
  659. goto out_bad;
  660. }
  661. mutex_unlock(&dir->i_mutex);
  662. return 0;
  663. out_bad:
  664. __rpc_depopulate(parent, files, start, eof);
  665. mutex_unlock(&dir->i_mutex);
  666. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  667. __FILE__, __func__, parent->d_name.name);
  668. return err;
  669. }
  670. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  671. struct qstr *name, umode_t mode, void *private,
  672. int (*populate)(struct dentry *, void *), void *args_populate)
  673. {
  674. struct dentry *dentry;
  675. struct inode *dir = parent->d_inode;
  676. int error;
  677. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  678. dentry = __rpc_lookup_create_exclusive(parent, name);
  679. if (IS_ERR(dentry))
  680. goto out;
  681. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  682. if (error != 0)
  683. goto out_err;
  684. if (populate != NULL) {
  685. error = populate(dentry, args_populate);
  686. if (error)
  687. goto err_rmdir;
  688. }
  689. out:
  690. mutex_unlock(&dir->i_mutex);
  691. return dentry;
  692. err_rmdir:
  693. __rpc_rmdir(dir, dentry);
  694. out_err:
  695. dentry = ERR_PTR(error);
  696. goto out;
  697. }
  698. static int rpc_rmdir_depopulate(struct dentry *dentry,
  699. void (*depopulate)(struct dentry *))
  700. {
  701. struct dentry *parent;
  702. struct inode *dir;
  703. int error;
  704. parent = dget_parent(dentry);
  705. dir = parent->d_inode;
  706. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  707. if (depopulate != NULL)
  708. depopulate(dentry);
  709. error = __rpc_rmdir(dir, dentry);
  710. mutex_unlock(&dir->i_mutex);
  711. dput(parent);
  712. return error;
  713. }
  714. /**
  715. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  716. * @parent: dentry of directory to create new "pipe" in
  717. * @name: name of pipe
  718. * @private: private data to associate with the pipe, for the caller's use
  719. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  720. * release_pipe, open_pipe, and destroy_msg.
  721. * @flags: rpc_pipe flags
  722. *
  723. * Data is made available for userspace to read by calls to
  724. * rpc_queue_upcall(). The actual reads will result in calls to
  725. * @ops->upcall, which will be called with the file pointer,
  726. * message, and userspace buffer to copy to.
  727. *
  728. * Writes can come at any time, and do not necessarily have to be
  729. * responses to upcalls. They will result in calls to @msg->downcall.
  730. *
  731. * The @private argument passed here will be available to all these methods
  732. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  733. */
  734. struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
  735. void *private, const struct rpc_pipe_ops *ops,
  736. int flags)
  737. {
  738. struct dentry *dentry;
  739. struct inode *dir = parent->d_inode;
  740. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  741. struct qstr q;
  742. int err;
  743. if (ops->upcall == NULL)
  744. umode &= ~S_IRUGO;
  745. if (ops->downcall == NULL)
  746. umode &= ~S_IWUGO;
  747. q.name = name;
  748. q.len = strlen(name);
  749. q.hash = full_name_hash(q.name, q.len),
  750. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  751. dentry = __rpc_lookup_create_exclusive(parent, &q);
  752. if (IS_ERR(dentry))
  753. goto out;
  754. err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
  755. private, ops, flags);
  756. if (err)
  757. goto out_err;
  758. out:
  759. mutex_unlock(&dir->i_mutex);
  760. return dentry;
  761. out_err:
  762. dentry = ERR_PTR(err);
  763. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  764. __FILE__, __func__, parent->d_name.name, name,
  765. err);
  766. goto out;
  767. }
  768. EXPORT_SYMBOL_GPL(rpc_mkpipe);
  769. /**
  770. * rpc_unlink - remove a pipe
  771. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  772. *
  773. * After this call, lookups will no longer find the pipe, and any
  774. * attempts to read or write using preexisting opens of the pipe will
  775. * return -EPIPE.
  776. */
  777. int
  778. rpc_unlink(struct dentry *dentry)
  779. {
  780. struct dentry *parent;
  781. struct inode *dir;
  782. int error = 0;
  783. parent = dget_parent(dentry);
  784. dir = parent->d_inode;
  785. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  786. error = __rpc_rmpipe(dir, dentry);
  787. mutex_unlock(&dir->i_mutex);
  788. dput(parent);
  789. return error;
  790. }
  791. EXPORT_SYMBOL_GPL(rpc_unlink);
  792. enum {
  793. RPCAUTH_info,
  794. RPCAUTH_EOF
  795. };
  796. static const struct rpc_filelist authfiles[] = {
  797. [RPCAUTH_info] = {
  798. .name = "info",
  799. .i_fop = &rpc_info_operations,
  800. .mode = S_IFREG | S_IRUSR,
  801. },
  802. };
  803. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  804. {
  805. return rpc_populate(dentry,
  806. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  807. private);
  808. }
  809. static void rpc_clntdir_depopulate(struct dentry *dentry)
  810. {
  811. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  812. }
  813. /**
  814. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  815. * @dentry: dentry from the rpc_pipefs root to the new directory
  816. * @name: &struct qstr for the name
  817. * @rpc_client: rpc client to associate with this directory
  818. *
  819. * This creates a directory at the given @path associated with
  820. * @rpc_clnt, which will contain a file named "info" with some basic
  821. * information about the client, together with any "pipes" that may
  822. * later be created using rpc_mkpipe().
  823. */
  824. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  825. struct qstr *name,
  826. struct rpc_clnt *rpc_client)
  827. {
  828. return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  829. rpc_clntdir_populate, rpc_client);
  830. }
  831. /**
  832. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  833. * @dentry: directory to remove
  834. */
  835. int rpc_remove_client_dir(struct dentry *dentry)
  836. {
  837. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  838. }
  839. static const struct rpc_filelist cache_pipefs_files[3] = {
  840. [0] = {
  841. .name = "channel",
  842. .i_fop = &cache_file_operations_pipefs,
  843. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  844. },
  845. [1] = {
  846. .name = "content",
  847. .i_fop = &content_file_operations_pipefs,
  848. .mode = S_IFREG|S_IRUSR,
  849. },
  850. [2] = {
  851. .name = "flush",
  852. .i_fop = &cache_flush_operations_pipefs,
  853. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  854. },
  855. };
  856. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  857. {
  858. return rpc_populate(dentry,
  859. cache_pipefs_files, 0, 3,
  860. private);
  861. }
  862. static void rpc_cachedir_depopulate(struct dentry *dentry)
  863. {
  864. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  865. }
  866. struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
  867. umode_t umode, struct cache_detail *cd)
  868. {
  869. return rpc_mkdir_populate(parent, name, umode, NULL,
  870. rpc_cachedir_populate, cd);
  871. }
  872. void rpc_remove_cache_dir(struct dentry *dentry)
  873. {
  874. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  875. }
  876. /*
  877. * populate the filesystem
  878. */
  879. static const struct super_operations s_ops = {
  880. .alloc_inode = rpc_alloc_inode,
  881. .destroy_inode = rpc_destroy_inode,
  882. .statfs = simple_statfs,
  883. };
  884. #define RPCAUTH_GSSMAGIC 0x67596969
  885. /*
  886. * We have a single directory with 1 node in it.
  887. */
  888. enum {
  889. RPCAUTH_lockd,
  890. RPCAUTH_mount,
  891. RPCAUTH_nfs,
  892. RPCAUTH_portmap,
  893. RPCAUTH_statd,
  894. RPCAUTH_nfsd4_cb,
  895. RPCAUTH_cache,
  896. RPCAUTH_RootEOF
  897. };
  898. static const struct rpc_filelist files[] = {
  899. [RPCAUTH_lockd] = {
  900. .name = "lockd",
  901. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  902. },
  903. [RPCAUTH_mount] = {
  904. .name = "mount",
  905. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  906. },
  907. [RPCAUTH_nfs] = {
  908. .name = "nfs",
  909. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  910. },
  911. [RPCAUTH_portmap] = {
  912. .name = "portmap",
  913. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  914. },
  915. [RPCAUTH_statd] = {
  916. .name = "statd",
  917. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  918. },
  919. [RPCAUTH_nfsd4_cb] = {
  920. .name = "nfsd4_cb",
  921. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  922. },
  923. [RPCAUTH_cache] = {
  924. .name = "cache",
  925. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  926. },
  927. };
  928. /*
  929. * This call can be used only in RPC pipefs mount notification hooks.
  930. */
  931. struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
  932. const unsigned char *dir_name)
  933. {
  934. struct qstr dir = {
  935. .name = dir_name,
  936. .len = strlen(dir_name),
  937. .hash = full_name_hash(dir_name, strlen(dir_name)),
  938. };
  939. return d_lookup(sb->s_root, &dir);
  940. }
  941. EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
  942. void rpc_pipefs_init_net(struct net *net)
  943. {
  944. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  945. mutex_init(&sn->pipefs_sb_lock);
  946. }
  947. /*
  948. * This call will be used for per network namespace operations calls.
  949. * Note: Function will be returned with pipefs_sb_lock taken if superblock was
  950. * found. This lock have to be released by rpc_put_sb_net() when all operations
  951. * will be completed.
  952. */
  953. struct super_block *rpc_get_sb_net(const struct net *net)
  954. {
  955. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  956. mutex_lock(&sn->pipefs_sb_lock);
  957. if (sn->pipefs_sb)
  958. return sn->pipefs_sb;
  959. mutex_unlock(&sn->pipefs_sb_lock);
  960. return NULL;
  961. }
  962. EXPORT_SYMBOL_GPL(rpc_get_sb_net);
  963. void rpc_put_sb_net(const struct net *net)
  964. {
  965. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  966. BUG_ON(sn->pipefs_sb == NULL);
  967. mutex_unlock(&sn->pipefs_sb_lock);
  968. }
  969. EXPORT_SYMBOL_GPL(rpc_put_sb_net);
  970. static int
  971. rpc_fill_super(struct super_block *sb, void *data, int silent)
  972. {
  973. struct inode *inode;
  974. struct dentry *root;
  975. struct net *net = data;
  976. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  977. int err;
  978. sb->s_blocksize = PAGE_CACHE_SIZE;
  979. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  980. sb->s_magic = RPCAUTH_GSSMAGIC;
  981. sb->s_op = &s_ops;
  982. sb->s_time_gran = 1;
  983. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  984. if (!inode)
  985. return -ENOMEM;
  986. sb->s_root = root = d_alloc_root(inode);
  987. if (!root) {
  988. iput(inode);
  989. return -ENOMEM;
  990. }
  991. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  992. return -ENOMEM;
  993. dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
  994. NET_NAME(net));
  995. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  996. RPC_PIPEFS_MOUNT,
  997. sb);
  998. if (err)
  999. goto err_depopulate;
  1000. sb->s_fs_info = get_net(net);
  1001. sn->pipefs_sb = sb;
  1002. return 0;
  1003. err_depopulate:
  1004. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1005. RPC_PIPEFS_UMOUNT,
  1006. sb);
  1007. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1008. return err;
  1009. }
  1010. static struct dentry *
  1011. rpc_mount(struct file_system_type *fs_type,
  1012. int flags, const char *dev_name, void *data)
  1013. {
  1014. return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
  1015. }
  1016. void rpc_kill_sb(struct super_block *sb)
  1017. {
  1018. struct net *net = sb->s_fs_info;
  1019. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1020. mutex_lock(&sn->pipefs_sb_lock);
  1021. sn->pipefs_sb = NULL;
  1022. mutex_unlock(&sn->pipefs_sb_lock);
  1023. put_net(net);
  1024. dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
  1025. NET_NAME(net));
  1026. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1027. RPC_PIPEFS_UMOUNT,
  1028. sb);
  1029. kill_litter_super(sb);
  1030. }
  1031. static struct file_system_type rpc_pipe_fs_type = {
  1032. .owner = THIS_MODULE,
  1033. .name = "rpc_pipefs",
  1034. .mount = rpc_mount,
  1035. .kill_sb = rpc_kill_sb,
  1036. };
  1037. static void
  1038. init_once(void *foo)
  1039. {
  1040. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1041. inode_init_once(&rpci->vfs_inode);
  1042. rpci->private = NULL;
  1043. rpci->pipe = NULL;
  1044. }
  1045. int register_rpc_pipefs(void)
  1046. {
  1047. int err;
  1048. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1049. sizeof(struct rpc_inode),
  1050. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1051. SLAB_MEM_SPREAD),
  1052. init_once);
  1053. if (!rpc_inode_cachep)
  1054. return -ENOMEM;
  1055. err = register_filesystem(&rpc_pipe_fs_type);
  1056. if (err) {
  1057. kmem_cache_destroy(rpc_inode_cachep);
  1058. return err;
  1059. }
  1060. return 0;
  1061. }
  1062. void unregister_rpc_pipefs(void)
  1063. {
  1064. kmem_cache_destroy(rpc_inode_cachep);
  1065. unregister_filesystem(&rpc_pipe_fs_type);
  1066. }
  1067. /* Make 'mount -t rpc_pipefs ...' autoload this module. */
  1068. MODULE_ALIAS("rpc_pipefs");