rpc_pipe.c 27 KB

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