rpc_pipe.c 29 KB

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