rpc_pipe.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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(&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
  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(&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(&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 int
  282. rpc_pipe_ioctl(struct inode *ino, struct file *filp,
  283. unsigned int cmd, unsigned long arg)
  284. {
  285. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  286. int len;
  287. switch (cmd) {
  288. case FIONREAD:
  289. if (rpci->ops == NULL)
  290. return -EPIPE;
  291. len = rpci->pipelen;
  292. if (filp->private_data) {
  293. struct rpc_pipe_msg *msg;
  294. msg = (struct rpc_pipe_msg *)filp->private_data;
  295. len += msg->len - msg->copied;
  296. }
  297. return put_user(len, (int __user *)arg);
  298. default:
  299. return -EINVAL;
  300. }
  301. }
  302. static const struct file_operations rpc_pipe_fops = {
  303. .owner = THIS_MODULE,
  304. .llseek = no_llseek,
  305. .read = rpc_pipe_read,
  306. .write = rpc_pipe_write,
  307. .poll = rpc_pipe_poll,
  308. .ioctl = rpc_pipe_ioctl,
  309. .open = rpc_pipe_open,
  310. .release = rpc_pipe_release,
  311. };
  312. static int
  313. rpc_show_info(struct seq_file *m, void *v)
  314. {
  315. struct rpc_clnt *clnt = m->private;
  316. seq_printf(m, "RPC server: %s\n", clnt->cl_server);
  317. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
  318. clnt->cl_prog, clnt->cl_vers);
  319. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  320. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  321. seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
  322. return 0;
  323. }
  324. static int
  325. rpc_info_open(struct inode *inode, struct file *file)
  326. {
  327. struct rpc_clnt *clnt;
  328. int ret = single_open(file, rpc_show_info, NULL);
  329. if (!ret) {
  330. struct seq_file *m = file->private_data;
  331. mutex_lock(&inode->i_mutex);
  332. clnt = RPC_I(inode)->private;
  333. if (clnt) {
  334. kref_get(&clnt->cl_kref);
  335. m->private = clnt;
  336. } else {
  337. single_release(inode, file);
  338. ret = -EINVAL;
  339. }
  340. mutex_unlock(&inode->i_mutex);
  341. }
  342. return ret;
  343. }
  344. static int
  345. rpc_info_release(struct inode *inode, struct file *file)
  346. {
  347. struct seq_file *m = file->private_data;
  348. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  349. if (clnt)
  350. rpc_release_client(clnt);
  351. return single_release(inode, file);
  352. }
  353. static const struct file_operations rpc_info_operations = {
  354. .owner = THIS_MODULE,
  355. .open = rpc_info_open,
  356. .read = seq_read,
  357. .llseek = seq_lseek,
  358. .release = rpc_info_release,
  359. };
  360. /*
  361. * Description of fs contents.
  362. */
  363. struct rpc_filelist {
  364. const char *name;
  365. const struct file_operations *i_fop;
  366. umode_t mode;
  367. };
  368. struct vfsmount *rpc_get_mount(void)
  369. {
  370. int err;
  371. err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
  372. if (err != 0)
  373. return ERR_PTR(err);
  374. return rpc_mount;
  375. }
  376. EXPORT_SYMBOL_GPL(rpc_get_mount);
  377. void rpc_put_mount(void)
  378. {
  379. simple_release_fs(&rpc_mount, &rpc_mount_count);
  380. }
  381. EXPORT_SYMBOL_GPL(rpc_put_mount);
  382. static int rpc_delete_dentry(struct dentry *dentry)
  383. {
  384. return 1;
  385. }
  386. static const struct dentry_operations rpc_dentry_operations = {
  387. .d_delete = rpc_delete_dentry,
  388. };
  389. static struct inode *
  390. rpc_get_inode(struct super_block *sb, umode_t mode)
  391. {
  392. struct inode *inode = new_inode(sb);
  393. if (!inode)
  394. return NULL;
  395. inode->i_mode = mode;
  396. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  397. switch(mode & S_IFMT) {
  398. case S_IFDIR:
  399. inode->i_fop = &simple_dir_operations;
  400. inode->i_op = &simple_dir_inode_operations;
  401. inc_nlink(inode);
  402. default:
  403. break;
  404. }
  405. return inode;
  406. }
  407. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  408. umode_t mode,
  409. const struct file_operations *i_fop,
  410. void *private)
  411. {
  412. struct inode *inode;
  413. BUG_ON(!d_unhashed(dentry));
  414. inode = rpc_get_inode(dir->i_sb, mode);
  415. if (!inode)
  416. goto out_err;
  417. inode->i_ino = iunique(dir->i_sb, 100);
  418. if (i_fop)
  419. inode->i_fop = i_fop;
  420. if (private)
  421. rpc_inode_setowner(inode, private);
  422. d_add(dentry, inode);
  423. return 0;
  424. out_err:
  425. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  426. __FILE__, __func__, dentry->d_name.name);
  427. dput(dentry);
  428. return -ENOMEM;
  429. }
  430. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  431. umode_t mode,
  432. const struct file_operations *i_fop,
  433. void *private)
  434. {
  435. int err;
  436. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  437. if (err)
  438. return err;
  439. fsnotify_create(dir, dentry);
  440. return 0;
  441. }
  442. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  443. umode_t mode,
  444. const struct file_operations *i_fop,
  445. void *private)
  446. {
  447. int err;
  448. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  449. if (err)
  450. return err;
  451. inc_nlink(dir);
  452. fsnotify_mkdir(dir, dentry);
  453. return 0;
  454. }
  455. static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
  456. umode_t mode,
  457. const struct file_operations *i_fop,
  458. void *private,
  459. const struct rpc_pipe_ops *ops,
  460. int flags)
  461. {
  462. struct rpc_inode *rpci;
  463. int err;
  464. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  465. if (err)
  466. return err;
  467. rpci = RPC_I(dentry->d_inode);
  468. rpci->nkern_readwriters = 1;
  469. rpci->private = private;
  470. rpci->flags = flags;
  471. rpci->ops = ops;
  472. fsnotify_create(dir, dentry);
  473. return 0;
  474. }
  475. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  476. {
  477. int ret;
  478. dget(dentry);
  479. ret = simple_rmdir(dir, dentry);
  480. d_delete(dentry);
  481. dput(dentry);
  482. return ret;
  483. }
  484. static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
  485. {
  486. int ret;
  487. dget(dentry);
  488. ret = simple_unlink(dir, dentry);
  489. d_delete(dentry);
  490. dput(dentry);
  491. return ret;
  492. }
  493. static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
  494. {
  495. struct inode *inode = dentry->d_inode;
  496. struct rpc_inode *rpci = RPC_I(inode);
  497. rpci->nkern_readwriters--;
  498. if (rpci->nkern_readwriters != 0)
  499. return 0;
  500. rpc_close_pipes(inode);
  501. return __rpc_unlink(dir, dentry);
  502. }
  503. static struct dentry *__rpc_lookup_create(struct dentry *parent,
  504. struct qstr *name)
  505. {
  506. struct dentry *dentry;
  507. dentry = d_lookup(parent, name);
  508. if (!dentry) {
  509. dentry = d_alloc(parent, name);
  510. if (!dentry) {
  511. dentry = ERR_PTR(-ENOMEM);
  512. goto out_err;
  513. }
  514. }
  515. if (!dentry->d_inode)
  516. dentry->d_op = &rpc_dentry_operations;
  517. out_err:
  518. return dentry;
  519. }
  520. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  521. struct qstr *name)
  522. {
  523. struct dentry *dentry;
  524. dentry = __rpc_lookup_create(parent, name);
  525. if (dentry->d_inode == NULL)
  526. return dentry;
  527. dput(dentry);
  528. return ERR_PTR(-EEXIST);
  529. }
  530. /*
  531. * FIXME: This probably has races.
  532. */
  533. static void __rpc_depopulate(struct dentry *parent,
  534. const struct rpc_filelist *files,
  535. int start, int eof)
  536. {
  537. struct inode *dir = parent->d_inode;
  538. struct dentry *dentry;
  539. struct qstr name;
  540. int i;
  541. for (i = start; i < eof; i++) {
  542. name.name = files[i].name;
  543. name.len = strlen(files[i].name);
  544. name.hash = full_name_hash(name.name, name.len);
  545. dentry = d_lookup(parent, &name);
  546. if (dentry == NULL)
  547. continue;
  548. if (dentry->d_inode == NULL)
  549. goto next;
  550. switch (dentry->d_inode->i_mode & S_IFMT) {
  551. default:
  552. BUG();
  553. case S_IFREG:
  554. __rpc_unlink(dir, dentry);
  555. break;
  556. case S_IFDIR:
  557. __rpc_rmdir(dir, dentry);
  558. }
  559. next:
  560. dput(dentry);
  561. }
  562. }
  563. static void rpc_depopulate(struct dentry *parent,
  564. const struct rpc_filelist *files,
  565. int start, int eof)
  566. {
  567. struct inode *dir = parent->d_inode;
  568. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  569. __rpc_depopulate(parent, files, start, eof);
  570. mutex_unlock(&dir->i_mutex);
  571. }
  572. static int rpc_populate(struct dentry *parent,
  573. const struct rpc_filelist *files,
  574. int start, int eof,
  575. void *private)
  576. {
  577. struct inode *dir = parent->d_inode;
  578. struct dentry *dentry;
  579. int i, err;
  580. mutex_lock(&dir->i_mutex);
  581. for (i = start; i < eof; i++) {
  582. struct qstr q;
  583. q.name = files[i].name;
  584. q.len = strlen(files[i].name);
  585. q.hash = full_name_hash(q.name, q.len);
  586. dentry = __rpc_lookup_create_exclusive(parent, &q);
  587. err = PTR_ERR(dentry);
  588. if (IS_ERR(dentry))
  589. goto out_bad;
  590. switch (files[i].mode & S_IFMT) {
  591. default:
  592. BUG();
  593. case S_IFREG:
  594. err = __rpc_create(dir, dentry,
  595. files[i].mode,
  596. files[i].i_fop,
  597. private);
  598. break;
  599. case S_IFDIR:
  600. err = __rpc_mkdir(dir, dentry,
  601. files[i].mode,
  602. NULL,
  603. private);
  604. }
  605. if (err != 0)
  606. goto out_bad;
  607. }
  608. mutex_unlock(&dir->i_mutex);
  609. return 0;
  610. out_bad:
  611. __rpc_depopulate(parent, files, start, eof);
  612. mutex_unlock(&dir->i_mutex);
  613. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  614. __FILE__, __func__, parent->d_name.name);
  615. return err;
  616. }
  617. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  618. struct qstr *name, umode_t mode, void *private,
  619. int (*populate)(struct dentry *, void *), void *args_populate)
  620. {
  621. struct dentry *dentry;
  622. struct inode *dir = parent->d_inode;
  623. int error;
  624. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  625. dentry = __rpc_lookup_create_exclusive(parent, name);
  626. if (IS_ERR(dentry))
  627. goto out;
  628. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  629. if (error != 0)
  630. goto out_err;
  631. if (populate != NULL) {
  632. error = populate(dentry, args_populate);
  633. if (error)
  634. goto err_rmdir;
  635. }
  636. out:
  637. mutex_unlock(&dir->i_mutex);
  638. return dentry;
  639. err_rmdir:
  640. __rpc_rmdir(dir, dentry);
  641. out_err:
  642. dentry = ERR_PTR(error);
  643. goto out;
  644. }
  645. static int rpc_rmdir_depopulate(struct dentry *dentry,
  646. void (*depopulate)(struct dentry *))
  647. {
  648. struct dentry *parent;
  649. struct inode *dir;
  650. int error;
  651. parent = dget_parent(dentry);
  652. dir = parent->d_inode;
  653. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  654. if (depopulate != NULL)
  655. depopulate(dentry);
  656. error = __rpc_rmdir(dir, dentry);
  657. mutex_unlock(&dir->i_mutex);
  658. dput(parent);
  659. return error;
  660. }
  661. /**
  662. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  663. * @parent: dentry of directory to create new "pipe" in
  664. * @name: name of pipe
  665. * @private: private data to associate with the pipe, for the caller's use
  666. * @ops: operations defining the behavior of the pipe: upcall, downcall,
  667. * release_pipe, open_pipe, and destroy_msg.
  668. * @flags: rpc_inode flags
  669. *
  670. * Data is made available for userspace to read by calls to
  671. * rpc_queue_upcall(). The actual reads will result in calls to
  672. * @ops->upcall, which will be called with the file pointer,
  673. * message, and userspace buffer to copy to.
  674. *
  675. * Writes can come at any time, and do not necessarily have to be
  676. * responses to upcalls. They will result in calls to @msg->downcall.
  677. *
  678. * The @private argument passed here will be available to all these methods
  679. * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
  680. */
  681. struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
  682. void *private, const struct rpc_pipe_ops *ops,
  683. int flags)
  684. {
  685. struct dentry *dentry;
  686. struct inode *dir = parent->d_inode;
  687. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  688. struct qstr q;
  689. int err;
  690. if (ops->upcall == NULL)
  691. umode &= ~S_IRUGO;
  692. if (ops->downcall == NULL)
  693. umode &= ~S_IWUGO;
  694. q.name = name;
  695. q.len = strlen(name);
  696. q.hash = full_name_hash(q.name, q.len),
  697. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  698. dentry = __rpc_lookup_create(parent, &q);
  699. if (IS_ERR(dentry))
  700. goto out;
  701. if (dentry->d_inode) {
  702. struct rpc_inode *rpci = RPC_I(dentry->d_inode);
  703. if (rpci->private != private ||
  704. rpci->ops != ops ||
  705. rpci->flags != flags) {
  706. dput (dentry);
  707. err = -EBUSY;
  708. goto out_err;
  709. }
  710. rpci->nkern_readwriters++;
  711. goto out;
  712. }
  713. err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
  714. private, ops, flags);
  715. if (err)
  716. goto out_err;
  717. out:
  718. mutex_unlock(&dir->i_mutex);
  719. return dentry;
  720. out_err:
  721. dentry = ERR_PTR(err);
  722. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  723. __FILE__, __func__, parent->d_name.name, name,
  724. err);
  725. goto out;
  726. }
  727. EXPORT_SYMBOL_GPL(rpc_mkpipe);
  728. /**
  729. * rpc_unlink - remove a pipe
  730. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  731. *
  732. * After this call, lookups will no longer find the pipe, and any
  733. * attempts to read or write using preexisting opens of the pipe will
  734. * return -EPIPE.
  735. */
  736. int
  737. rpc_unlink(struct dentry *dentry)
  738. {
  739. struct dentry *parent;
  740. struct inode *dir;
  741. int error = 0;
  742. parent = dget_parent(dentry);
  743. dir = parent->d_inode;
  744. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  745. error = __rpc_rmpipe(dir, dentry);
  746. mutex_unlock(&dir->i_mutex);
  747. dput(parent);
  748. return error;
  749. }
  750. EXPORT_SYMBOL_GPL(rpc_unlink);
  751. enum {
  752. RPCAUTH_info,
  753. RPCAUTH_EOF
  754. };
  755. static const struct rpc_filelist authfiles[] = {
  756. [RPCAUTH_info] = {
  757. .name = "info",
  758. .i_fop = &rpc_info_operations,
  759. .mode = S_IFREG | S_IRUSR,
  760. },
  761. };
  762. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  763. {
  764. return rpc_populate(dentry,
  765. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  766. private);
  767. }
  768. static void rpc_clntdir_depopulate(struct dentry *dentry)
  769. {
  770. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  771. }
  772. /**
  773. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  774. * @path: path from the rpc_pipefs root to the new directory
  775. * @rpc_client: rpc client to associate with this directory
  776. *
  777. * This creates a directory at the given @path associated with
  778. * @rpc_clnt, which will contain a file named "info" with some basic
  779. * information about the client, together with any "pipes" that may
  780. * later be created using rpc_mkpipe().
  781. */
  782. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  783. struct qstr *name,
  784. struct rpc_clnt *rpc_client)
  785. {
  786. return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  787. rpc_clntdir_populate, rpc_client);
  788. }
  789. /**
  790. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  791. * @dentry: directory to remove
  792. */
  793. int rpc_remove_client_dir(struct dentry *dentry)
  794. {
  795. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  796. }
  797. static const struct rpc_filelist cache_pipefs_files[3] = {
  798. [0] = {
  799. .name = "channel",
  800. .i_fop = &cache_file_operations_pipefs,
  801. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  802. },
  803. [1] = {
  804. .name = "content",
  805. .i_fop = &content_file_operations_pipefs,
  806. .mode = S_IFREG|S_IRUSR,
  807. },
  808. [2] = {
  809. .name = "flush",
  810. .i_fop = &cache_flush_operations_pipefs,
  811. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  812. },
  813. };
  814. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  815. {
  816. return rpc_populate(dentry,
  817. cache_pipefs_files, 0, 3,
  818. private);
  819. }
  820. static void rpc_cachedir_depopulate(struct dentry *dentry)
  821. {
  822. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  823. }
  824. struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
  825. mode_t umode, struct cache_detail *cd)
  826. {
  827. return rpc_mkdir_populate(parent, name, umode, NULL,
  828. rpc_cachedir_populate, cd);
  829. }
  830. void rpc_remove_cache_dir(struct dentry *dentry)
  831. {
  832. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  833. }
  834. /*
  835. * populate the filesystem
  836. */
  837. static const struct super_operations s_ops = {
  838. .alloc_inode = rpc_alloc_inode,
  839. .destroy_inode = rpc_destroy_inode,
  840. .statfs = simple_statfs,
  841. };
  842. #define RPCAUTH_GSSMAGIC 0x67596969
  843. /*
  844. * We have a single directory with 1 node in it.
  845. */
  846. enum {
  847. RPCAUTH_lockd,
  848. RPCAUTH_mount,
  849. RPCAUTH_nfs,
  850. RPCAUTH_portmap,
  851. RPCAUTH_statd,
  852. RPCAUTH_nfsd4_cb,
  853. RPCAUTH_cache,
  854. RPCAUTH_RootEOF
  855. };
  856. static const struct rpc_filelist files[] = {
  857. [RPCAUTH_lockd] = {
  858. .name = "lockd",
  859. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  860. },
  861. [RPCAUTH_mount] = {
  862. .name = "mount",
  863. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  864. },
  865. [RPCAUTH_nfs] = {
  866. .name = "nfs",
  867. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  868. },
  869. [RPCAUTH_portmap] = {
  870. .name = "portmap",
  871. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  872. },
  873. [RPCAUTH_statd] = {
  874. .name = "statd",
  875. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  876. },
  877. [RPCAUTH_nfsd4_cb] = {
  878. .name = "nfsd4_cb",
  879. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  880. },
  881. [RPCAUTH_cache] = {
  882. .name = "cache",
  883. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  884. },
  885. };
  886. static int
  887. rpc_fill_super(struct super_block *sb, void *data, int silent)
  888. {
  889. struct inode *inode;
  890. struct dentry *root;
  891. sb->s_blocksize = PAGE_CACHE_SIZE;
  892. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  893. sb->s_magic = RPCAUTH_GSSMAGIC;
  894. sb->s_op = &s_ops;
  895. sb->s_time_gran = 1;
  896. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  897. if (!inode)
  898. return -ENOMEM;
  899. root = d_alloc_root(inode);
  900. if (!root) {
  901. iput(inode);
  902. return -ENOMEM;
  903. }
  904. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  905. goto out;
  906. sb->s_root = root;
  907. return 0;
  908. out:
  909. d_genocide(root);
  910. dput(root);
  911. return -ENOMEM;
  912. }
  913. static int
  914. rpc_get_sb(struct file_system_type *fs_type,
  915. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  916. {
  917. return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
  918. }
  919. static struct file_system_type rpc_pipe_fs_type = {
  920. .owner = THIS_MODULE,
  921. .name = "rpc_pipefs",
  922. .get_sb = rpc_get_sb,
  923. .kill_sb = kill_litter_super,
  924. };
  925. static void
  926. init_once(void *foo)
  927. {
  928. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  929. inode_init_once(&rpci->vfs_inode);
  930. rpci->private = NULL;
  931. rpci->nreaders = 0;
  932. rpci->nwriters = 0;
  933. INIT_LIST_HEAD(&rpci->in_upcall);
  934. INIT_LIST_HEAD(&rpci->in_downcall);
  935. INIT_LIST_HEAD(&rpci->pipe);
  936. rpci->pipelen = 0;
  937. init_waitqueue_head(&rpci->waitq);
  938. INIT_DELAYED_WORK(&rpci->queue_timeout,
  939. rpc_timeout_upcall_queue);
  940. rpci->ops = NULL;
  941. }
  942. int register_rpc_pipefs(void)
  943. {
  944. int err;
  945. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  946. sizeof(struct rpc_inode),
  947. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  948. SLAB_MEM_SPREAD),
  949. init_once);
  950. if (!rpc_inode_cachep)
  951. return -ENOMEM;
  952. err = register_filesystem(&rpc_pipe_fs_type);
  953. if (err) {
  954. kmem_cache_destroy(rpc_inode_cachep);
  955. return err;
  956. }
  957. return 0;
  958. }
  959. void unregister_rpc_pipefs(void)
  960. {
  961. kmem_cache_destroy(rpc_inode_cachep);
  962. unregister_filesystem(&rpc_pipe_fs_type);
  963. }