rpc_pipe.c 25 KB

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