rpc_pipe.c 25 KB

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