rpc_pipe.c 25 KB

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