rpc_pipe.c 29 KB

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