rpc_pipe.c 26 KB

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