rpc_pipe.c 25 KB

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