rpc_pipe.c 28 KB

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