rpc_pipe.c 25 KB

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