rpc_pipe.c 28 KB

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