rpc_pipe.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. /**
  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 = filp->private_data;
  185. if (msg != NULL) {
  186. spin_lock(&inode->i_lock);
  187. msg->errno = -EAGAIN;
  188. list_del_init(&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_init(&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 = 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 = NULL;
  332. int ret = single_open(file, rpc_show_info, NULL);
  333. if (!ret) {
  334. struct seq_file *m = file->private_data;
  335. spin_lock(&file->f_path.dentry->d_lock);
  336. if (!d_unhashed(file->f_path.dentry))
  337. clnt = RPC_I(inode)->private;
  338. if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
  339. spin_unlock(&file->f_path.dentry->d_lock);
  340. m->private = clnt;
  341. } else {
  342. spin_unlock(&file->f_path.dentry->d_lock);
  343. single_release(inode, file);
  344. ret = -EINVAL;
  345. }
  346. }
  347. return ret;
  348. }
  349. static int
  350. rpc_info_release(struct inode *inode, struct file *file)
  351. {
  352. struct seq_file *m = file->private_data;
  353. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  354. if (clnt)
  355. rpc_release_client(clnt);
  356. return single_release(inode, file);
  357. }
  358. static const struct file_operations rpc_info_operations = {
  359. .owner = THIS_MODULE,
  360. .open = rpc_info_open,
  361. .read = seq_read,
  362. .llseek = seq_lseek,
  363. .release = rpc_info_release,
  364. };
  365. /*
  366. * Description of fs contents.
  367. */
  368. struct rpc_filelist {
  369. const char *name;
  370. const struct file_operations *i_fop;
  371. umode_t mode;
  372. };
  373. struct vfsmount *rpc_get_mount(void)
  374. {
  375. int err;
  376. err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
  377. if (err != 0)
  378. return ERR_PTR(err);
  379. return rpc_mnt;
  380. }
  381. EXPORT_SYMBOL_GPL(rpc_get_mount);
  382. void rpc_put_mount(void)
  383. {
  384. simple_release_fs(&rpc_mnt, &rpc_mount_count);
  385. }
  386. EXPORT_SYMBOL_GPL(rpc_put_mount);
  387. static int rpc_delete_dentry(struct dentry *dentry)
  388. {
  389. return 1;
  390. }
  391. static const struct dentry_operations rpc_dentry_operations = {
  392. .d_delete = rpc_delete_dentry,
  393. };
  394. static struct inode *
  395. rpc_get_inode(struct super_block *sb, umode_t mode)
  396. {
  397. struct inode *inode = new_inode(sb);
  398. if (!inode)
  399. return NULL;
  400. inode->i_ino = get_next_ino();
  401. inode->i_mode = mode;
  402. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  403. switch(mode & S_IFMT) {
  404. case S_IFDIR:
  405. inode->i_fop = &simple_dir_operations;
  406. inode->i_op = &simple_dir_inode_operations;
  407. inc_nlink(inode);
  408. default:
  409. break;
  410. }
  411. return inode;
  412. }
  413. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  414. umode_t mode,
  415. const struct file_operations *i_fop,
  416. void *private)
  417. {
  418. struct inode *inode;
  419. BUG_ON(!d_unhashed(dentry));
  420. inode = rpc_get_inode(dir->i_sb, mode);
  421. if (!inode)
  422. goto out_err;
  423. inode->i_ino = iunique(dir->i_sb, 100);
  424. if (i_fop)
  425. inode->i_fop = i_fop;
  426. if (private)
  427. rpc_inode_setowner(inode, private);
  428. d_add(dentry, inode);
  429. return 0;
  430. out_err:
  431. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  432. __FILE__, __func__, dentry->d_name.name);
  433. dput(dentry);
  434. return -ENOMEM;
  435. }
  436. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  437. umode_t mode,
  438. const struct file_operations *i_fop,
  439. void *private)
  440. {
  441. int err;
  442. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  443. if (err)
  444. return err;
  445. fsnotify_create(dir, dentry);
  446. return 0;
  447. }
  448. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  449. umode_t mode,
  450. const struct file_operations *i_fop,
  451. void *private)
  452. {
  453. int err;
  454. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  455. if (err)
  456. return err;
  457. inc_nlink(dir);
  458. fsnotify_mkdir(dir, dentry);
  459. return 0;
  460. }
  461. static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
  462. umode_t mode,
  463. const struct file_operations *i_fop,
  464. void *private,
  465. const struct rpc_pipe_ops *ops,
  466. int flags)
  467. {
  468. struct rpc_inode *rpci;
  469. int err;
  470. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  471. if (err)
  472. return err;
  473. rpci = RPC_I(dentry->d_inode);
  474. rpci->nkern_readwriters = 1;
  475. rpci->private = private;
  476. rpci->flags = flags;
  477. rpci->ops = ops;
  478. fsnotify_create(dir, dentry);
  479. return 0;
  480. }
  481. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  482. {
  483. int ret;
  484. dget(dentry);
  485. ret = simple_rmdir(dir, dentry);
  486. d_delete(dentry);
  487. dput(dentry);
  488. return ret;
  489. }
  490. static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
  491. {
  492. int ret;
  493. dget(dentry);
  494. ret = simple_unlink(dir, dentry);
  495. d_delete(dentry);
  496. dput(dentry);
  497. return ret;
  498. }
  499. static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
  500. {
  501. struct inode *inode = dentry->d_inode;
  502. struct rpc_inode *rpci = RPC_I(inode);
  503. rpci->nkern_readwriters--;
  504. if (rpci->nkern_readwriters != 0)
  505. return 0;
  506. rpc_close_pipes(inode);
  507. return __rpc_unlink(dir, dentry);
  508. }
  509. static struct dentry *__rpc_lookup_create(struct dentry *parent,
  510. struct qstr *name)
  511. {
  512. struct dentry *dentry;
  513. dentry = d_lookup(parent, name);
  514. if (!dentry) {
  515. dentry = d_alloc(parent, name);
  516. if (!dentry) {
  517. dentry = ERR_PTR(-ENOMEM);
  518. goto out_err;
  519. }
  520. }
  521. if (!dentry->d_inode)
  522. dentry->d_op = &rpc_dentry_operations;
  523. out_err:
  524. return dentry;
  525. }
  526. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  527. struct qstr *name)
  528. {
  529. struct dentry *dentry;
  530. dentry = __rpc_lookup_create(parent, name);
  531. if (IS_ERR(dentry))
  532. return dentry;
  533. if (dentry->d_inode == NULL)
  534. return dentry;
  535. dput(dentry);
  536. return ERR_PTR(-EEXIST);
  537. }
  538. /*
  539. * FIXME: This probably has races.
  540. */
  541. static void __rpc_depopulate(struct dentry *parent,
  542. const struct rpc_filelist *files,
  543. int start, int eof)
  544. {
  545. struct inode *dir = parent->d_inode;
  546. struct dentry *dentry;
  547. struct qstr name;
  548. int i;
  549. for (i = start; i < eof; i++) {
  550. name.name = files[i].name;
  551. name.len = strlen(files[i].name);
  552. name.hash = full_name_hash(name.name, name.len);
  553. dentry = d_lookup(parent, &name);
  554. if (dentry == NULL)
  555. continue;
  556. if (dentry->d_inode == NULL)
  557. goto next;
  558. switch (dentry->d_inode->i_mode & S_IFMT) {
  559. default:
  560. BUG();
  561. case S_IFREG:
  562. __rpc_unlink(dir, dentry);
  563. break;
  564. case S_IFDIR:
  565. __rpc_rmdir(dir, dentry);
  566. }
  567. next:
  568. dput(dentry);
  569. }
  570. }
  571. static void rpc_depopulate(struct dentry *parent,
  572. const struct rpc_filelist *files,
  573. int start, int eof)
  574. {
  575. struct inode *dir = parent->d_inode;
  576. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  577. __rpc_depopulate(parent, files, start, eof);
  578. mutex_unlock(&dir->i_mutex);
  579. }
  580. static int rpc_populate(struct dentry *parent,
  581. const struct rpc_filelist *files,
  582. int start, int eof,
  583. void *private)
  584. {
  585. struct inode *dir = parent->d_inode;
  586. struct dentry *dentry;
  587. int i, err;
  588. mutex_lock(&dir->i_mutex);
  589. for (i = start; i < eof; i++) {
  590. struct qstr q;
  591. q.name = files[i].name;
  592. q.len = strlen(files[i].name);
  593. q.hash = full_name_hash(q.name, q.len);
  594. dentry = __rpc_lookup_create_exclusive(parent, &q);
  595. err = PTR_ERR(dentry);
  596. if (IS_ERR(dentry))
  597. goto out_bad;
  598. switch (files[i].mode & S_IFMT) {
  599. default:
  600. BUG();
  601. case S_IFREG:
  602. err = __rpc_create(dir, dentry,
  603. files[i].mode,
  604. files[i].i_fop,
  605. private);
  606. break;
  607. case S_IFDIR:
  608. err = __rpc_mkdir(dir, dentry,
  609. files[i].mode,
  610. NULL,
  611. private);
  612. }
  613. if (err != 0)
  614. goto out_bad;
  615. }
  616. mutex_unlock(&dir->i_mutex);
  617. return 0;
  618. out_bad:
  619. __rpc_depopulate(parent, files, start, eof);
  620. mutex_unlock(&dir->i_mutex);
  621. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  622. __FILE__, __func__, parent->d_name.name);
  623. return err;
  624. }
  625. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  626. struct qstr *name, umode_t mode, void *private,
  627. int (*populate)(struct dentry *, void *), void *args_populate)
  628. {
  629. struct dentry *dentry;
  630. struct inode *dir = parent->d_inode;
  631. int error;
  632. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  633. dentry = __rpc_lookup_create_exclusive(parent, name);
  634. if (IS_ERR(dentry))
  635. goto out;
  636. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  637. if (error != 0)
  638. goto out_err;
  639. if (populate != NULL) {
  640. error = populate(dentry, args_populate);
  641. if (error)
  642. goto err_rmdir;
  643. }
  644. out:
  645. mutex_unlock(&dir->i_mutex);
  646. return dentry;
  647. err_rmdir:
  648. __rpc_rmdir(dir, dentry);
  649. out_err:
  650. dentry = ERR_PTR(error);
  651. goto out;
  652. }
  653. static int rpc_rmdir_depopulate(struct dentry *dentry,
  654. void (*depopulate)(struct dentry *))
  655. {
  656. struct dentry *parent;
  657. struct inode *dir;
  658. int error;
  659. parent = dget_parent(dentry);
  660. dir = parent->d_inode;
  661. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  662. if (depopulate != NULL)
  663. depopulate(dentry);
  664. error = __rpc_rmdir(dir, dentry);
  665. mutex_unlock(&dir->i_mutex);
  666. dput(parent);
  667. return error;
  668. }
  669. /**
  670. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  671. * @parent: dentry of directory to create new "pipe" in
  672. * @name: name of pipe
  673. * @private: private data to associate with the pipe, for the caller's use
  674. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  675. * release_pipe, open_pipe, and destroy_msg.
  676. * @flags: rpc_inode flags
  677. *
  678. * Data is made available for userspace to read by calls to
  679. * rpc_queue_upcall(). The actual reads will result in calls to
  680. * @ops->upcall, which will be called with the file pointer,
  681. * message, and userspace buffer to copy to.
  682. *
  683. * Writes can come at any time, and do not necessarily have to be
  684. * responses to upcalls. They will result in calls to @msg->downcall.
  685. *
  686. * The @private argument passed here will be available to all these methods
  687. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  688. */
  689. struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
  690. void *private, const struct rpc_pipe_ops *ops,
  691. int flags)
  692. {
  693. struct dentry *dentry;
  694. struct inode *dir = parent->d_inode;
  695. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  696. struct qstr q;
  697. int err;
  698. if (ops->upcall == NULL)
  699. umode &= ~S_IRUGO;
  700. if (ops->downcall == NULL)
  701. umode &= ~S_IWUGO;
  702. q.name = name;
  703. q.len = strlen(name);
  704. q.hash = full_name_hash(q.name, q.len),
  705. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  706. dentry = __rpc_lookup_create(parent, &q);
  707. if (IS_ERR(dentry))
  708. goto out;
  709. if (dentry->d_inode) {
  710. struct rpc_inode *rpci = RPC_I(dentry->d_inode);
  711. if (rpci->private != private ||
  712. rpci->ops != ops ||
  713. rpci->flags != flags) {
  714. dput (dentry);
  715. err = -EBUSY;
  716. goto out_err;
  717. }
  718. rpci->nkern_readwriters++;
  719. goto out;
  720. }
  721. err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
  722. private, ops, flags);
  723. if (err)
  724. goto out_err;
  725. out:
  726. mutex_unlock(&dir->i_mutex);
  727. return dentry;
  728. out_err:
  729. dentry = ERR_PTR(err);
  730. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  731. __FILE__, __func__, parent->d_name.name, name,
  732. err);
  733. goto out;
  734. }
  735. EXPORT_SYMBOL_GPL(rpc_mkpipe);
  736. /**
  737. * rpc_unlink - remove a pipe
  738. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  739. *
  740. * After this call, lookups will no longer find the pipe, and any
  741. * attempts to read or write using preexisting opens of the pipe will
  742. * return -EPIPE.
  743. */
  744. int
  745. rpc_unlink(struct dentry *dentry)
  746. {
  747. struct dentry *parent;
  748. struct inode *dir;
  749. int error = 0;
  750. parent = dget_parent(dentry);
  751. dir = parent->d_inode;
  752. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  753. error = __rpc_rmpipe(dir, dentry);
  754. mutex_unlock(&dir->i_mutex);
  755. dput(parent);
  756. return error;
  757. }
  758. EXPORT_SYMBOL_GPL(rpc_unlink);
  759. enum {
  760. RPCAUTH_info,
  761. RPCAUTH_EOF
  762. };
  763. static const struct rpc_filelist authfiles[] = {
  764. [RPCAUTH_info] = {
  765. .name = "info",
  766. .i_fop = &rpc_info_operations,
  767. .mode = S_IFREG | S_IRUSR,
  768. },
  769. };
  770. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  771. {
  772. return rpc_populate(dentry,
  773. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  774. private);
  775. }
  776. static void rpc_clntdir_depopulate(struct dentry *dentry)
  777. {
  778. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  779. }
  780. /**
  781. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  782. * @dentry: dentry from the rpc_pipefs root to the new directory
  783. * @name: &struct qstr for the name
  784. * @rpc_client: rpc client to associate with this directory
  785. *
  786. * This creates a directory at the given @path associated with
  787. * @rpc_clnt, which will contain a file named "info" with some basic
  788. * information about the client, together with any "pipes" that may
  789. * later be created using rpc_mkpipe().
  790. */
  791. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  792. struct qstr *name,
  793. struct rpc_clnt *rpc_client)
  794. {
  795. return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  796. rpc_clntdir_populate, rpc_client);
  797. }
  798. /**
  799. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  800. * @dentry: directory to remove
  801. */
  802. int rpc_remove_client_dir(struct dentry *dentry)
  803. {
  804. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  805. }
  806. static const struct rpc_filelist cache_pipefs_files[3] = {
  807. [0] = {
  808. .name = "channel",
  809. .i_fop = &cache_file_operations_pipefs,
  810. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  811. },
  812. [1] = {
  813. .name = "content",
  814. .i_fop = &content_file_operations_pipefs,
  815. .mode = S_IFREG|S_IRUSR,
  816. },
  817. [2] = {
  818. .name = "flush",
  819. .i_fop = &cache_flush_operations_pipefs,
  820. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  821. },
  822. };
  823. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  824. {
  825. return rpc_populate(dentry,
  826. cache_pipefs_files, 0, 3,
  827. private);
  828. }
  829. static void rpc_cachedir_depopulate(struct dentry *dentry)
  830. {
  831. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  832. }
  833. struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
  834. mode_t umode, struct cache_detail *cd)
  835. {
  836. return rpc_mkdir_populate(parent, name, umode, NULL,
  837. rpc_cachedir_populate, cd);
  838. }
  839. void rpc_remove_cache_dir(struct dentry *dentry)
  840. {
  841. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  842. }
  843. /*
  844. * populate the filesystem
  845. */
  846. static const struct super_operations s_ops = {
  847. .alloc_inode = rpc_alloc_inode,
  848. .destroy_inode = rpc_destroy_inode,
  849. .statfs = simple_statfs,
  850. };
  851. #define RPCAUTH_GSSMAGIC 0x67596969
  852. /*
  853. * We have a single directory with 1 node in it.
  854. */
  855. enum {
  856. RPCAUTH_lockd,
  857. RPCAUTH_mount,
  858. RPCAUTH_nfs,
  859. RPCAUTH_portmap,
  860. RPCAUTH_statd,
  861. RPCAUTH_nfsd4_cb,
  862. RPCAUTH_cache,
  863. RPCAUTH_RootEOF
  864. };
  865. static const struct rpc_filelist files[] = {
  866. [RPCAUTH_lockd] = {
  867. .name = "lockd",
  868. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  869. },
  870. [RPCAUTH_mount] = {
  871. .name = "mount",
  872. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  873. },
  874. [RPCAUTH_nfs] = {
  875. .name = "nfs",
  876. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  877. },
  878. [RPCAUTH_portmap] = {
  879. .name = "portmap",
  880. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  881. },
  882. [RPCAUTH_statd] = {
  883. .name = "statd",
  884. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  885. },
  886. [RPCAUTH_nfsd4_cb] = {
  887. .name = "nfsd4_cb",
  888. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  889. },
  890. [RPCAUTH_cache] = {
  891. .name = "cache",
  892. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  893. },
  894. };
  895. static int
  896. rpc_fill_super(struct super_block *sb, void *data, int silent)
  897. {
  898. struct inode *inode;
  899. struct dentry *root;
  900. sb->s_blocksize = PAGE_CACHE_SIZE;
  901. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  902. sb->s_magic = RPCAUTH_GSSMAGIC;
  903. sb->s_op = &s_ops;
  904. sb->s_time_gran = 1;
  905. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  906. if (!inode)
  907. return -ENOMEM;
  908. sb->s_root = root = d_alloc_root(inode);
  909. if (!root) {
  910. iput(inode);
  911. return -ENOMEM;
  912. }
  913. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  914. return -ENOMEM;
  915. return 0;
  916. }
  917. static struct dentry *
  918. rpc_mount(struct file_system_type *fs_type,
  919. int flags, const char *dev_name, void *data)
  920. {
  921. return mount_single(fs_type, flags, data, rpc_fill_super);
  922. }
  923. static struct file_system_type rpc_pipe_fs_type = {
  924. .owner = THIS_MODULE,
  925. .name = "rpc_pipefs",
  926. .mount = rpc_mount,
  927. .kill_sb = kill_litter_super,
  928. };
  929. static void
  930. init_once(void *foo)
  931. {
  932. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  933. inode_init_once(&rpci->vfs_inode);
  934. rpci->private = NULL;
  935. rpci->nreaders = 0;
  936. rpci->nwriters = 0;
  937. INIT_LIST_HEAD(&rpci->in_upcall);
  938. INIT_LIST_HEAD(&rpci->in_downcall);
  939. INIT_LIST_HEAD(&rpci->pipe);
  940. rpci->pipelen = 0;
  941. init_waitqueue_head(&rpci->waitq);
  942. INIT_DELAYED_WORK(&rpci->queue_timeout,
  943. rpc_timeout_upcall_queue);
  944. rpci->ops = NULL;
  945. }
  946. int register_rpc_pipefs(void)
  947. {
  948. int err;
  949. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  950. sizeof(struct rpc_inode),
  951. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  952. SLAB_MEM_SPREAD),
  953. init_once);
  954. if (!rpc_inode_cachep)
  955. return -ENOMEM;
  956. err = register_filesystem(&rpc_pipe_fs_type);
  957. if (err) {
  958. kmem_cache_destroy(rpc_inode_cachep);
  959. return err;
  960. }
  961. return 0;
  962. }
  963. void unregister_rpc_pipefs(void)
  964. {
  965. kmem_cache_destroy(rpc_inode_cachep);
  966. unregister_filesystem(&rpc_pipe_fs_type);
  967. }