rpc_pipe.c 25 KB

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