rpc_pipe.c 29 KB

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