rpc_pipe.c 25 KB

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