rpc_pipe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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
  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 (dentry->d_inode == NULL)
  526. return dentry;
  527. dput(dentry);
  528. return ERR_PTR(-EEXIST);
  529. }
  530. /*
  531. * FIXME: This probably has races.
  532. */
  533. static void __rpc_depopulate(struct dentry *parent,
  534. const struct rpc_filelist *files,
  535. int start, int eof)
  536. {
  537. struct inode *dir = parent->d_inode;
  538. struct dentry *dentry;
  539. struct qstr name;
  540. int i;
  541. for (i = start; i < eof; i++) {
  542. name.name = files[i].name;
  543. name.len = strlen(files[i].name);
  544. name.hash = full_name_hash(name.name, name.len);
  545. dentry = d_lookup(parent, &name);
  546. if (dentry == NULL)
  547. continue;
  548. if (dentry->d_inode == NULL)
  549. goto next;
  550. switch (dentry->d_inode->i_mode & S_IFMT) {
  551. default:
  552. BUG();
  553. case S_IFREG:
  554. __rpc_unlink(dir, dentry);
  555. break;
  556. case S_IFDIR:
  557. __rpc_rmdir(dir, dentry);
  558. }
  559. next:
  560. dput(dentry);
  561. }
  562. }
  563. static void rpc_depopulate(struct dentry *parent,
  564. const struct rpc_filelist *files,
  565. int start, int eof)
  566. {
  567. struct inode *dir = parent->d_inode;
  568. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  569. __rpc_depopulate(parent, files, start, eof);
  570. mutex_unlock(&dir->i_mutex);
  571. }
  572. static int rpc_populate(struct dentry *parent,
  573. const struct rpc_filelist *files,
  574. int start, int eof,
  575. void *private)
  576. {
  577. struct inode *dir = parent->d_inode;
  578. struct dentry *dentry;
  579. int i, err;
  580. mutex_lock(&dir->i_mutex);
  581. for (i = start; i < eof; i++) {
  582. struct qstr q;
  583. q.name = files[i].name;
  584. q.len = strlen(files[i].name);
  585. q.hash = full_name_hash(q.name, q.len);
  586. dentry = __rpc_lookup_create_exclusive(parent, &q);
  587. err = PTR_ERR(dentry);
  588. if (IS_ERR(dentry))
  589. goto out_bad;
  590. switch (files[i].mode & S_IFMT) {
  591. default:
  592. BUG();
  593. case S_IFREG:
  594. err = __rpc_create(dir, dentry,
  595. files[i].mode,
  596. files[i].i_fop,
  597. private);
  598. break;
  599. case S_IFDIR:
  600. err = __rpc_mkdir(dir, dentry,
  601. files[i].mode,
  602. NULL,
  603. private);
  604. }
  605. if (err != 0)
  606. goto out_bad;
  607. }
  608. mutex_unlock(&dir->i_mutex);
  609. return 0;
  610. out_bad:
  611. __rpc_depopulate(parent, files, start, eof);
  612. mutex_unlock(&dir->i_mutex);
  613. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  614. __FILE__, __func__, parent->d_name.name);
  615. return err;
  616. }
  617. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  618. struct qstr *name, umode_t mode, void *private,
  619. int (*populate)(struct dentry *, void *), void *args_populate)
  620. {
  621. struct dentry *dentry;
  622. struct inode *dir = parent->d_inode;
  623. int error;
  624. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  625. dentry = __rpc_lookup_create_exclusive(parent, name);
  626. if (IS_ERR(dentry))
  627. goto out;
  628. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  629. if (error != 0)
  630. goto out_err;
  631. if (populate != NULL) {
  632. error = populate(dentry, args_populate);
  633. if (error)
  634. goto err_rmdir;
  635. }
  636. out:
  637. mutex_unlock(&dir->i_mutex);
  638. return dentry;
  639. err_rmdir:
  640. __rpc_rmdir(dir, dentry);
  641. out_err:
  642. dentry = ERR_PTR(error);
  643. goto out;
  644. }
  645. static int rpc_rmdir_depopulate(struct dentry *dentry,
  646. void (*depopulate)(struct dentry *))
  647. {
  648. struct dentry *parent;
  649. struct inode *dir;
  650. int error;
  651. parent = dget_parent(dentry);
  652. dir = parent->d_inode;
  653. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  654. if (depopulate != NULL)
  655. depopulate(dentry);
  656. error = __rpc_rmdir(dir, dentry);
  657. mutex_unlock(&dir->i_mutex);
  658. dput(parent);
  659. return error;
  660. }
  661. /**
  662. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  663. * @parent: dentry of directory to create new "pipe" in
  664. * @name: name of pipe
  665. * @private: private data to associate with the pipe, for the caller's use
  666. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  667. * release_pipe, open_pipe, and destroy_msg.
  668. * @flags: rpc_inode flags
  669. *
  670. * Data is made available for userspace to read by calls to
  671. * rpc_queue_upcall(). The actual reads will result in calls to
  672. * @ops->upcall, which will be called with the file pointer,
  673. * message, and userspace buffer to copy to.
  674. *
  675. * Writes can come at any time, and do not necessarily have to be
  676. * responses to upcalls. They will result in calls to @msg->downcall.
  677. *
  678. * The @private argument passed here will be available to all these methods
  679. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  680. */
  681. struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
  682. void *private, const struct rpc_pipe_ops *ops,
  683. int flags)
  684. {
  685. struct dentry *dentry;
  686. struct inode *dir = parent->d_inode;
  687. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  688. struct qstr q;
  689. int err;
  690. if (ops->upcall == NULL)
  691. umode &= ~S_IRUGO;
  692. if (ops->downcall == NULL)
  693. umode &= ~S_IWUGO;
  694. q.name = name;
  695. q.len = strlen(name);
  696. q.hash = full_name_hash(q.name, q.len),
  697. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  698. dentry = __rpc_lookup_create(parent, &q);
  699. if (IS_ERR(dentry))
  700. goto out;
  701. if (dentry->d_inode) {
  702. struct rpc_inode *rpci = RPC_I(dentry->d_inode);
  703. if (rpci->private != private ||
  704. rpci->ops != ops ||
  705. rpci->flags != flags) {
  706. dput (dentry);
  707. err = -EBUSY;
  708. goto out_err;
  709. }
  710. rpci->nkern_readwriters++;
  711. goto out;
  712. }
  713. err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
  714. private, ops, flags);
  715. if (err)
  716. goto out_err;
  717. out:
  718. mutex_unlock(&dir->i_mutex);
  719. return dentry;
  720. out_err:
  721. dentry = ERR_PTR(err);
  722. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  723. __FILE__, __func__, parent->d_name.name, name,
  724. err);
  725. goto out;
  726. }
  727. EXPORT_SYMBOL_GPL(rpc_mkpipe);
  728. /**
  729. * rpc_unlink - remove a pipe
  730. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  731. *
  732. * After this call, lookups will no longer find the pipe, and any
  733. * attempts to read or write using preexisting opens of the pipe will
  734. * return -EPIPE.
  735. */
  736. int
  737. rpc_unlink(struct dentry *dentry)
  738. {
  739. struct dentry *parent;
  740. struct inode *dir;
  741. int error = 0;
  742. parent = dget_parent(dentry);
  743. dir = parent->d_inode;
  744. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  745. error = __rpc_rmpipe(dir, dentry);
  746. mutex_unlock(&dir->i_mutex);
  747. dput(parent);
  748. return error;
  749. }
  750. EXPORT_SYMBOL_GPL(rpc_unlink);
  751. enum {
  752. RPCAUTH_info,
  753. RPCAUTH_EOF
  754. };
  755. static const struct rpc_filelist authfiles[] = {
  756. [RPCAUTH_info] = {
  757. .name = "info",
  758. .i_fop = &rpc_info_operations,
  759. .mode = S_IFREG | S_IRUSR,
  760. },
  761. };
  762. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  763. {
  764. return rpc_populate(dentry,
  765. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  766. private);
  767. }
  768. static void rpc_clntdir_depopulate(struct dentry *dentry)
  769. {
  770. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  771. }
  772. /**
  773. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  774. * @dentry: dentry from the rpc_pipefs root to the new directory
  775. * @name: &struct qstr for the name
  776. * @rpc_client: rpc client to associate with this directory
  777. *
  778. * This creates a directory at the given @path associated with
  779. * @rpc_clnt, which will contain a file named "info" with some basic
  780. * information about the client, together with any "pipes" that may
  781. * later be created using rpc_mkpipe().
  782. */
  783. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  784. struct qstr *name,
  785. struct rpc_clnt *rpc_client)
  786. {
  787. return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  788. rpc_clntdir_populate, rpc_client);
  789. }
  790. /**
  791. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  792. * @dentry: directory to remove
  793. */
  794. int rpc_remove_client_dir(struct dentry *dentry)
  795. {
  796. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  797. }
  798. static const struct rpc_filelist cache_pipefs_files[3] = {
  799. [0] = {
  800. .name = "channel",
  801. .i_fop = &cache_file_operations_pipefs,
  802. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  803. },
  804. [1] = {
  805. .name = "content",
  806. .i_fop = &content_file_operations_pipefs,
  807. .mode = S_IFREG|S_IRUSR,
  808. },
  809. [2] = {
  810. .name = "flush",
  811. .i_fop = &cache_flush_operations_pipefs,
  812. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  813. },
  814. };
  815. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  816. {
  817. return rpc_populate(dentry,
  818. cache_pipefs_files, 0, 3,
  819. private);
  820. }
  821. static void rpc_cachedir_depopulate(struct dentry *dentry)
  822. {
  823. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  824. }
  825. struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
  826. mode_t umode, struct cache_detail *cd)
  827. {
  828. return rpc_mkdir_populate(parent, name, umode, NULL,
  829. rpc_cachedir_populate, cd);
  830. }
  831. void rpc_remove_cache_dir(struct dentry *dentry)
  832. {
  833. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  834. }
  835. /*
  836. * populate the filesystem
  837. */
  838. static const struct super_operations s_ops = {
  839. .alloc_inode = rpc_alloc_inode,
  840. .destroy_inode = rpc_destroy_inode,
  841. .statfs = simple_statfs,
  842. };
  843. #define RPCAUTH_GSSMAGIC 0x67596969
  844. /*
  845. * We have a single directory with 1 node in it.
  846. */
  847. enum {
  848. RPCAUTH_lockd,
  849. RPCAUTH_mount,
  850. RPCAUTH_nfs,
  851. RPCAUTH_portmap,
  852. RPCAUTH_statd,
  853. RPCAUTH_nfsd4_cb,
  854. RPCAUTH_cache,
  855. RPCAUTH_RootEOF
  856. };
  857. static const struct rpc_filelist files[] = {
  858. [RPCAUTH_lockd] = {
  859. .name = "lockd",
  860. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  861. },
  862. [RPCAUTH_mount] = {
  863. .name = "mount",
  864. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  865. },
  866. [RPCAUTH_nfs] = {
  867. .name = "nfs",
  868. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  869. },
  870. [RPCAUTH_portmap] = {
  871. .name = "portmap",
  872. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  873. },
  874. [RPCAUTH_statd] = {
  875. .name = "statd",
  876. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  877. },
  878. [RPCAUTH_nfsd4_cb] = {
  879. .name = "nfsd4_cb",
  880. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  881. },
  882. [RPCAUTH_cache] = {
  883. .name = "cache",
  884. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  885. },
  886. };
  887. static int
  888. rpc_fill_super(struct super_block *sb, void *data, int silent)
  889. {
  890. struct inode *inode;
  891. struct dentry *root;
  892. sb->s_blocksize = PAGE_CACHE_SIZE;
  893. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  894. sb->s_magic = RPCAUTH_GSSMAGIC;
  895. sb->s_op = &s_ops;
  896. sb->s_time_gran = 1;
  897. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  898. if (!inode)
  899. return -ENOMEM;
  900. root = d_alloc_root(inode);
  901. if (!root) {
  902. iput(inode);
  903. return -ENOMEM;
  904. }
  905. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  906. goto out;
  907. sb->s_root = root;
  908. return 0;
  909. out:
  910. d_genocide(root);
  911. dput(root);
  912. return -ENOMEM;
  913. }
  914. static int
  915. rpc_get_sb(struct file_system_type *fs_type,
  916. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  917. {
  918. return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
  919. }
  920. static struct file_system_type rpc_pipe_fs_type = {
  921. .owner = THIS_MODULE,
  922. .name = "rpc_pipefs",
  923. .get_sb = rpc_get_sb,
  924. .kill_sb = kill_litter_super,
  925. };
  926. static void
  927. init_once(void *foo)
  928. {
  929. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  930. inode_init_once(&rpci->vfs_inode);
  931. rpci->private = NULL;
  932. rpci->nreaders = 0;
  933. rpci->nwriters = 0;
  934. INIT_LIST_HEAD(&rpci->in_upcall);
  935. INIT_LIST_HEAD(&rpci->in_downcall);
  936. INIT_LIST_HEAD(&rpci->pipe);
  937. rpci->pipelen = 0;
  938. init_waitqueue_head(&rpci->waitq);
  939. INIT_DELAYED_WORK(&rpci->queue_timeout,
  940. rpc_timeout_upcall_queue);
  941. rpci->ops = NULL;
  942. }
  943. int register_rpc_pipefs(void)
  944. {
  945. int err;
  946. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  947. sizeof(struct rpc_inode),
  948. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  949. SLAB_MEM_SPREAD),
  950. init_once);
  951. if (!rpc_inode_cachep)
  952. return -ENOMEM;
  953. err = register_filesystem(&rpc_pipe_fs_type);
  954. if (err) {
  955. kmem_cache_destroy(rpc_inode_cachep);
  956. return err;
  957. }
  958. return 0;
  959. }
  960. void unregister_rpc_pipefs(void)
  961. {
  962. kmem_cache_destroy(rpc_inode_cachep);
  963. unregister_filesystem(&rpc_pipe_fs_type);
  964. }