rpc_pipe.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. * @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 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. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  734. * release_pipe, open_pipe, and destroy_msg.
  735. * @flags: rpc_pipe flags
  736. *
  737. * Data is made available for userspace to read by calls to
  738. * rpc_queue_upcall(). The actual reads will result in calls to
  739. * @ops->upcall, which will be called with the file pointer,
  740. * message, and userspace buffer to copy to.
  741. *
  742. * Writes can come at any time, and do not necessarily have to be
  743. * responses to upcalls. They will result in calls to @msg->downcall.
  744. *
  745. * The @private argument passed here will be available to all these methods
  746. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  747. */
  748. struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
  749. void *private, struct rpc_pipe *pipe)
  750. {
  751. struct dentry *dentry;
  752. struct inode *dir = parent->d_inode;
  753. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  754. struct qstr q;
  755. int err;
  756. if (pipe->ops->upcall == NULL)
  757. umode &= ~S_IRUGO;
  758. if (pipe->ops->downcall == NULL)
  759. umode &= ~S_IWUGO;
  760. q.name = name;
  761. q.len = strlen(name);
  762. q.hash = full_name_hash(q.name, q.len),
  763. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  764. dentry = __rpc_lookup_create_exclusive(parent, &q);
  765. if (IS_ERR(dentry))
  766. goto out;
  767. err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
  768. private, pipe);
  769. if (err)
  770. goto out_err;
  771. out:
  772. mutex_unlock(&dir->i_mutex);
  773. return dentry;
  774. out_err:
  775. dentry = ERR_PTR(err);
  776. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  777. __FILE__, __func__, parent->d_name.name, name,
  778. err);
  779. goto out;
  780. }
  781. EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
  782. /**
  783. * rpc_unlink - remove a pipe
  784. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  785. *
  786. * After this call, lookups will no longer find the pipe, and any
  787. * attempts to read or write using preexisting opens of the pipe will
  788. * return -EPIPE.
  789. */
  790. int
  791. rpc_unlink(struct dentry *dentry)
  792. {
  793. struct dentry *parent;
  794. struct inode *dir;
  795. int error = 0;
  796. parent = dget_parent(dentry);
  797. dir = parent->d_inode;
  798. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  799. error = __rpc_rmpipe(dir, dentry);
  800. mutex_unlock(&dir->i_mutex);
  801. dput(parent);
  802. return error;
  803. }
  804. EXPORT_SYMBOL_GPL(rpc_unlink);
  805. enum {
  806. RPCAUTH_info,
  807. RPCAUTH_EOF
  808. };
  809. static const struct rpc_filelist authfiles[] = {
  810. [RPCAUTH_info] = {
  811. .name = "info",
  812. .i_fop = &rpc_info_operations,
  813. .mode = S_IFREG | S_IRUSR,
  814. },
  815. };
  816. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  817. {
  818. return rpc_populate(dentry,
  819. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  820. private);
  821. }
  822. static void rpc_clntdir_depopulate(struct dentry *dentry)
  823. {
  824. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  825. }
  826. /**
  827. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  828. * @dentry: dentry from the rpc_pipefs root to the new directory
  829. * @name: &struct qstr for the name
  830. * @rpc_client: rpc client to associate with this directory
  831. *
  832. * This creates a directory at the given @path associated with
  833. * @rpc_clnt, which will contain a file named "info" with some basic
  834. * information about the client, together with any "pipes" that may
  835. * later be created using rpc_mkpipe().
  836. */
  837. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  838. struct qstr *name,
  839. struct rpc_clnt *rpc_client)
  840. {
  841. return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  842. rpc_clntdir_populate, rpc_client);
  843. }
  844. /**
  845. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  846. * @clnt: rpc client
  847. */
  848. int rpc_remove_client_dir(struct dentry *dentry)
  849. {
  850. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  851. }
  852. static const struct rpc_filelist cache_pipefs_files[3] = {
  853. [0] = {
  854. .name = "channel",
  855. .i_fop = &cache_file_operations_pipefs,
  856. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  857. },
  858. [1] = {
  859. .name = "content",
  860. .i_fop = &content_file_operations_pipefs,
  861. .mode = S_IFREG|S_IRUSR,
  862. },
  863. [2] = {
  864. .name = "flush",
  865. .i_fop = &cache_flush_operations_pipefs,
  866. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  867. },
  868. };
  869. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  870. {
  871. return rpc_populate(dentry,
  872. cache_pipefs_files, 0, 3,
  873. private);
  874. }
  875. static void rpc_cachedir_depopulate(struct dentry *dentry)
  876. {
  877. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  878. }
  879. struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
  880. umode_t umode, struct cache_detail *cd)
  881. {
  882. return rpc_mkdir_populate(parent, name, umode, NULL,
  883. rpc_cachedir_populate, cd);
  884. }
  885. void rpc_remove_cache_dir(struct dentry *dentry)
  886. {
  887. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  888. }
  889. /*
  890. * populate the filesystem
  891. */
  892. static const struct super_operations s_ops = {
  893. .alloc_inode = rpc_alloc_inode,
  894. .destroy_inode = rpc_destroy_inode,
  895. .statfs = simple_statfs,
  896. };
  897. #define RPCAUTH_GSSMAGIC 0x67596969
  898. /*
  899. * We have a single directory with 1 node in it.
  900. */
  901. enum {
  902. RPCAUTH_lockd,
  903. RPCAUTH_mount,
  904. RPCAUTH_nfs,
  905. RPCAUTH_portmap,
  906. RPCAUTH_statd,
  907. RPCAUTH_nfsd4_cb,
  908. RPCAUTH_cache,
  909. RPCAUTH_nfsd,
  910. RPCAUTH_RootEOF
  911. };
  912. static const struct rpc_filelist files[] = {
  913. [RPCAUTH_lockd] = {
  914. .name = "lockd",
  915. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  916. },
  917. [RPCAUTH_mount] = {
  918. .name = "mount",
  919. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  920. },
  921. [RPCAUTH_nfs] = {
  922. .name = "nfs",
  923. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  924. },
  925. [RPCAUTH_portmap] = {
  926. .name = "portmap",
  927. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  928. },
  929. [RPCAUTH_statd] = {
  930. .name = "statd",
  931. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  932. },
  933. [RPCAUTH_nfsd4_cb] = {
  934. .name = "nfsd4_cb",
  935. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  936. },
  937. [RPCAUTH_cache] = {
  938. .name = "cache",
  939. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  940. },
  941. [RPCAUTH_nfsd] = {
  942. .name = "nfsd",
  943. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  944. },
  945. };
  946. /*
  947. * This call can be used only in RPC pipefs mount notification hooks.
  948. */
  949. struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
  950. const unsigned char *dir_name)
  951. {
  952. struct qstr dir = {
  953. .name = dir_name,
  954. .len = strlen(dir_name),
  955. .hash = full_name_hash(dir_name, strlen(dir_name)),
  956. };
  957. return d_lookup(sb->s_root, &dir);
  958. }
  959. EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
  960. void rpc_pipefs_init_net(struct net *net)
  961. {
  962. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  963. mutex_init(&sn->pipefs_sb_lock);
  964. }
  965. /*
  966. * This call will be used for per network namespace operations calls.
  967. * Note: Function will be returned with pipefs_sb_lock taken if superblock was
  968. * found. This lock have to be released by rpc_put_sb_net() when all operations
  969. * will be completed.
  970. */
  971. struct super_block *rpc_get_sb_net(const struct net *net)
  972. {
  973. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  974. mutex_lock(&sn->pipefs_sb_lock);
  975. if (sn->pipefs_sb)
  976. return sn->pipefs_sb;
  977. mutex_unlock(&sn->pipefs_sb_lock);
  978. return NULL;
  979. }
  980. EXPORT_SYMBOL_GPL(rpc_get_sb_net);
  981. void rpc_put_sb_net(const struct net *net)
  982. {
  983. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  984. BUG_ON(sn->pipefs_sb == NULL);
  985. mutex_unlock(&sn->pipefs_sb_lock);
  986. }
  987. EXPORT_SYMBOL_GPL(rpc_put_sb_net);
  988. static int
  989. rpc_fill_super(struct super_block *sb, void *data, int silent)
  990. {
  991. struct inode *inode;
  992. struct dentry *root;
  993. struct net *net = data;
  994. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  995. int err;
  996. sb->s_blocksize = PAGE_CACHE_SIZE;
  997. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  998. sb->s_magic = RPCAUTH_GSSMAGIC;
  999. sb->s_op = &s_ops;
  1000. sb->s_time_gran = 1;
  1001. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  1002. sb->s_root = root = d_make_root(inode);
  1003. if (!root)
  1004. return -ENOMEM;
  1005. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  1006. return -ENOMEM;
  1007. dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
  1008. NET_NAME(net));
  1009. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1010. RPC_PIPEFS_MOUNT,
  1011. sb);
  1012. if (err)
  1013. goto err_depopulate;
  1014. sb->s_fs_info = get_net(net);
  1015. sn->pipefs_sb = sb;
  1016. return 0;
  1017. err_depopulate:
  1018. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1019. RPC_PIPEFS_UMOUNT,
  1020. sb);
  1021. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1022. return err;
  1023. }
  1024. static struct dentry *
  1025. rpc_mount(struct file_system_type *fs_type,
  1026. int flags, const char *dev_name, void *data)
  1027. {
  1028. return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
  1029. }
  1030. static void rpc_kill_sb(struct super_block *sb)
  1031. {
  1032. struct net *net = sb->s_fs_info;
  1033. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1034. mutex_lock(&sn->pipefs_sb_lock);
  1035. sn->pipefs_sb = NULL;
  1036. mutex_unlock(&sn->pipefs_sb_lock);
  1037. put_net(net);
  1038. dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
  1039. NET_NAME(net));
  1040. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1041. RPC_PIPEFS_UMOUNT,
  1042. sb);
  1043. kill_litter_super(sb);
  1044. }
  1045. static struct file_system_type rpc_pipe_fs_type = {
  1046. .owner = THIS_MODULE,
  1047. .name = "rpc_pipefs",
  1048. .mount = rpc_mount,
  1049. .kill_sb = rpc_kill_sb,
  1050. };
  1051. static void
  1052. init_once(void *foo)
  1053. {
  1054. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1055. inode_init_once(&rpci->vfs_inode);
  1056. rpci->private = NULL;
  1057. rpci->pipe = NULL;
  1058. init_waitqueue_head(&rpci->waitq);
  1059. }
  1060. int register_rpc_pipefs(void)
  1061. {
  1062. int err;
  1063. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1064. sizeof(struct rpc_inode),
  1065. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1066. SLAB_MEM_SPREAD),
  1067. init_once);
  1068. if (!rpc_inode_cachep)
  1069. return -ENOMEM;
  1070. err = rpc_clients_notifier_register();
  1071. if (err)
  1072. goto err_notifier;
  1073. err = register_filesystem(&rpc_pipe_fs_type);
  1074. if (err)
  1075. goto err_register;
  1076. return 0;
  1077. err_register:
  1078. rpc_clients_notifier_unregister();
  1079. err_notifier:
  1080. kmem_cache_destroy(rpc_inode_cachep);
  1081. return err;
  1082. }
  1083. void unregister_rpc_pipefs(void)
  1084. {
  1085. rpc_clients_notifier_unregister();
  1086. kmem_cache_destroy(rpc_inode_cachep);
  1087. unregister_filesystem(&rpc_pipe_fs_type);
  1088. }
  1089. /* Make 'mount -t rpc_pipefs ...' autoload this module. */
  1090. MODULE_ALIAS("rpc_pipefs");