rpc_pipe.c 25 KB

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