rpc_pipe.c 24 KB

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