rpc_pipe.c 29 KB

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