rpc_pipe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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/dnotify.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. int
  68. rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
  69. {
  70. struct rpc_inode *rpci = RPC_I(inode);
  71. int res = -EPIPE;
  72. spin_lock(&inode->i_lock);
  73. if (rpci->ops == NULL)
  74. goto out;
  75. if (rpci->nreaders) {
  76. list_add_tail(&msg->list, &rpci->pipe);
  77. rpci->pipelen += msg->len;
  78. res = 0;
  79. } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  80. if (list_empty(&rpci->pipe))
  81. queue_delayed_work(rpciod_workqueue,
  82. &rpci->queue_timeout,
  83. RPC_UPCALL_TIMEOUT);
  84. list_add_tail(&msg->list, &rpci->pipe);
  85. rpci->pipelen += msg->len;
  86. res = 0;
  87. }
  88. out:
  89. spin_unlock(&inode->i_lock);
  90. wake_up(&rpci->waitq);
  91. return res;
  92. }
  93. static inline void
  94. rpc_inode_setowner(struct inode *inode, void *private)
  95. {
  96. RPC_I(inode)->private = private;
  97. }
  98. static void
  99. rpc_close_pipes(struct inode *inode)
  100. {
  101. struct rpc_inode *rpci = RPC_I(inode);
  102. struct rpc_pipe_ops *ops;
  103. mutex_lock(&inode->i_mutex);
  104. ops = rpci->ops;
  105. if (ops != NULL) {
  106. LIST_HEAD(free_list);
  107. spin_lock(&inode->i_lock);
  108. rpci->nreaders = 0;
  109. list_splice_init(&rpci->in_upcall, &free_list);
  110. list_splice_init(&rpci->pipe, &free_list);
  111. rpci->pipelen = 0;
  112. rpci->ops = NULL;
  113. spin_unlock(&inode->i_lock);
  114. rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
  115. rpci->nwriters = 0;
  116. if (ops->release_pipe)
  117. ops->release_pipe(inode);
  118. cancel_delayed_work(&rpci->queue_timeout);
  119. flush_workqueue(rpciod_workqueue);
  120. }
  121. rpc_inode_setowner(inode, NULL);
  122. mutex_unlock(&inode->i_mutex);
  123. }
  124. static struct inode *
  125. rpc_alloc_inode(struct super_block *sb)
  126. {
  127. struct rpc_inode *rpci;
  128. rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
  129. if (!rpci)
  130. return NULL;
  131. return &rpci->vfs_inode;
  132. }
  133. static void
  134. rpc_destroy_inode(struct inode *inode)
  135. {
  136. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  137. }
  138. static int
  139. rpc_pipe_open(struct inode *inode, struct file *filp)
  140. {
  141. struct rpc_inode *rpci = RPC_I(inode);
  142. int res = -ENXIO;
  143. mutex_lock(&inode->i_mutex);
  144. if (rpci->ops != NULL) {
  145. if (filp->f_mode & FMODE_READ)
  146. rpci->nreaders ++;
  147. if (filp->f_mode & FMODE_WRITE)
  148. rpci->nwriters ++;
  149. res = 0;
  150. }
  151. mutex_unlock(&inode->i_mutex);
  152. return res;
  153. }
  154. static int
  155. rpc_pipe_release(struct inode *inode, struct file *filp)
  156. {
  157. struct rpc_inode *rpci = RPC_I(inode);
  158. struct rpc_pipe_msg *msg;
  159. mutex_lock(&inode->i_mutex);
  160. if (rpci->ops == NULL)
  161. goto out;
  162. msg = (struct rpc_pipe_msg *)filp->private_data;
  163. if (msg != NULL) {
  164. spin_lock(&inode->i_lock);
  165. msg->errno = -EAGAIN;
  166. list_del(&msg->list);
  167. spin_unlock(&inode->i_lock);
  168. rpci->ops->destroy_msg(msg);
  169. }
  170. if (filp->f_mode & FMODE_WRITE)
  171. rpci->nwriters --;
  172. if (filp->f_mode & FMODE_READ) {
  173. rpci->nreaders --;
  174. if (rpci->nreaders == 0) {
  175. LIST_HEAD(free_list);
  176. spin_lock(&inode->i_lock);
  177. list_splice_init(&rpci->pipe, &free_list);
  178. rpci->pipelen = 0;
  179. spin_unlock(&inode->i_lock);
  180. rpc_purge_list(rpci, &free_list,
  181. rpci->ops->destroy_msg, -EAGAIN);
  182. }
  183. }
  184. if (rpci->ops->release_pipe)
  185. rpci->ops->release_pipe(inode);
  186. out:
  187. mutex_unlock(&inode->i_mutex);
  188. return 0;
  189. }
  190. static ssize_t
  191. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  192. {
  193. struct inode *inode = filp->f_path.dentry->d_inode;
  194. struct rpc_inode *rpci = RPC_I(inode);
  195. struct rpc_pipe_msg *msg;
  196. int res = 0;
  197. mutex_lock(&inode->i_mutex);
  198. if (rpci->ops == NULL) {
  199. res = -EPIPE;
  200. goto out_unlock;
  201. }
  202. msg = filp->private_data;
  203. if (msg == NULL) {
  204. spin_lock(&inode->i_lock);
  205. if (!list_empty(&rpci->pipe)) {
  206. msg = list_entry(rpci->pipe.next,
  207. struct rpc_pipe_msg,
  208. list);
  209. list_move(&msg->list, &rpci->in_upcall);
  210. rpci->pipelen -= msg->len;
  211. filp->private_data = msg;
  212. msg->copied = 0;
  213. }
  214. spin_unlock(&inode->i_lock);
  215. if (msg == NULL)
  216. goto out_unlock;
  217. }
  218. /* NOTE: it is up to the callback to update msg->copied */
  219. res = rpci->ops->upcall(filp, msg, buf, len);
  220. if (res < 0 || msg->len == msg->copied) {
  221. filp->private_data = NULL;
  222. spin_lock(&inode->i_lock);
  223. list_del(&msg->list);
  224. spin_unlock(&inode->i_lock);
  225. rpci->ops->destroy_msg(msg);
  226. }
  227. out_unlock:
  228. mutex_unlock(&inode->i_mutex);
  229. return res;
  230. }
  231. static ssize_t
  232. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  233. {
  234. struct inode *inode = filp->f_path.dentry->d_inode;
  235. struct rpc_inode *rpci = RPC_I(inode);
  236. int res;
  237. mutex_lock(&inode->i_mutex);
  238. res = -EPIPE;
  239. if (rpci->ops != NULL)
  240. res = rpci->ops->downcall(filp, buf, len);
  241. mutex_unlock(&inode->i_mutex);
  242. return res;
  243. }
  244. static unsigned int
  245. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  246. {
  247. struct rpc_inode *rpci;
  248. unsigned int mask = 0;
  249. rpci = RPC_I(filp->f_path.dentry->d_inode);
  250. poll_wait(filp, &rpci->waitq, wait);
  251. mask = POLLOUT | POLLWRNORM;
  252. if (rpci->ops == NULL)
  253. mask |= POLLERR | POLLHUP;
  254. if (!list_empty(&rpci->pipe))
  255. mask |= POLLIN | POLLRDNORM;
  256. return mask;
  257. }
  258. static int
  259. rpc_pipe_ioctl(struct inode *ino, struct file *filp,
  260. unsigned int cmd, unsigned long arg)
  261. {
  262. struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
  263. int len;
  264. switch (cmd) {
  265. case FIONREAD:
  266. if (rpci->ops == NULL)
  267. return -EPIPE;
  268. len = rpci->pipelen;
  269. if (filp->private_data) {
  270. struct rpc_pipe_msg *msg;
  271. msg = (struct rpc_pipe_msg *)filp->private_data;
  272. len += msg->len - msg->copied;
  273. }
  274. return put_user(len, (int __user *)arg);
  275. default:
  276. return -EINVAL;
  277. }
  278. }
  279. static const struct file_operations rpc_pipe_fops = {
  280. .owner = THIS_MODULE,
  281. .llseek = no_llseek,
  282. .read = rpc_pipe_read,
  283. .write = rpc_pipe_write,
  284. .poll = rpc_pipe_poll,
  285. .ioctl = rpc_pipe_ioctl,
  286. .open = rpc_pipe_open,
  287. .release = rpc_pipe_release,
  288. };
  289. static int
  290. rpc_show_info(struct seq_file *m, void *v)
  291. {
  292. struct rpc_clnt *clnt = m->private;
  293. seq_printf(m, "RPC server: %s\n", clnt->cl_server);
  294. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
  295. clnt->cl_prog, clnt->cl_vers);
  296. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  297. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  298. return 0;
  299. }
  300. static int
  301. rpc_info_open(struct inode *inode, struct file *file)
  302. {
  303. struct rpc_clnt *clnt;
  304. int ret = single_open(file, rpc_show_info, NULL);
  305. if (!ret) {
  306. struct seq_file *m = file->private_data;
  307. mutex_lock(&inode->i_mutex);
  308. clnt = RPC_I(inode)->private;
  309. if (clnt) {
  310. kref_get(&clnt->cl_kref);
  311. m->private = clnt;
  312. } else {
  313. single_release(inode, file);
  314. ret = -EINVAL;
  315. }
  316. mutex_unlock(&inode->i_mutex);
  317. }
  318. return ret;
  319. }
  320. static int
  321. rpc_info_release(struct inode *inode, struct file *file)
  322. {
  323. struct seq_file *m = file->private_data;
  324. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  325. if (clnt)
  326. rpc_release_client(clnt);
  327. return single_release(inode, file);
  328. }
  329. static const struct file_operations rpc_info_operations = {
  330. .owner = THIS_MODULE,
  331. .open = rpc_info_open,
  332. .read = seq_read,
  333. .llseek = seq_lseek,
  334. .release = rpc_info_release,
  335. };
  336. /*
  337. * We have a single directory with 1 node in it.
  338. */
  339. enum {
  340. RPCAUTH_Root = 1,
  341. RPCAUTH_lockd,
  342. RPCAUTH_mount,
  343. RPCAUTH_nfs,
  344. RPCAUTH_portmap,
  345. RPCAUTH_statd,
  346. RPCAUTH_RootEOF
  347. };
  348. /*
  349. * Description of fs contents.
  350. */
  351. struct rpc_filelist {
  352. char *name;
  353. const struct file_operations *i_fop;
  354. int mode;
  355. };
  356. static struct rpc_filelist files[] = {
  357. [RPCAUTH_lockd] = {
  358. .name = "lockd",
  359. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  360. },
  361. [RPCAUTH_mount] = {
  362. .name = "mount",
  363. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  364. },
  365. [RPCAUTH_nfs] = {
  366. .name = "nfs",
  367. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  368. },
  369. [RPCAUTH_portmap] = {
  370. .name = "portmap",
  371. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  372. },
  373. [RPCAUTH_statd] = {
  374. .name = "statd",
  375. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  376. },
  377. };
  378. enum {
  379. RPCAUTH_info = 2,
  380. RPCAUTH_EOF
  381. };
  382. static struct rpc_filelist authfiles[] = {
  383. [RPCAUTH_info] = {
  384. .name = "info",
  385. .i_fop = &rpc_info_operations,
  386. .mode = S_IFREG | S_IRUSR,
  387. },
  388. };
  389. struct vfsmount *rpc_get_mount(void)
  390. {
  391. int err;
  392. err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
  393. if (err != 0)
  394. return ERR_PTR(err);
  395. return rpc_mount;
  396. }
  397. void rpc_put_mount(void)
  398. {
  399. simple_release_fs(&rpc_mount, &rpc_mount_count);
  400. }
  401. static int rpc_delete_dentry(struct dentry *dentry)
  402. {
  403. return 1;
  404. }
  405. static struct dentry_operations rpc_dentry_operations = {
  406. .d_delete = rpc_delete_dentry,
  407. };
  408. static int
  409. rpc_lookup_parent(char *path, struct nameidata *nd)
  410. {
  411. if (path[0] == '\0')
  412. return -ENOENT;
  413. nd->mnt = rpc_get_mount();
  414. if (IS_ERR(nd->mnt)) {
  415. printk(KERN_WARNING "%s: %s failed to mount "
  416. "pseudofilesystem \n", __FILE__, __FUNCTION__);
  417. return PTR_ERR(nd->mnt);
  418. }
  419. mntget(nd->mnt);
  420. nd->dentry = dget(rpc_mount->mnt_root);
  421. nd->last_type = LAST_ROOT;
  422. nd->flags = LOOKUP_PARENT;
  423. nd->depth = 0;
  424. if (path_walk(path, nd)) {
  425. printk(KERN_WARNING "%s: %s failed to find path %s\n",
  426. __FILE__, __FUNCTION__, path);
  427. rpc_put_mount();
  428. return -ENOENT;
  429. }
  430. return 0;
  431. }
  432. static void
  433. rpc_release_path(struct nameidata *nd)
  434. {
  435. path_release(nd);
  436. rpc_put_mount();
  437. }
  438. static struct inode *
  439. rpc_get_inode(struct super_block *sb, int mode)
  440. {
  441. struct inode *inode = new_inode(sb);
  442. if (!inode)
  443. return NULL;
  444. inode->i_mode = mode;
  445. inode->i_uid = inode->i_gid = 0;
  446. inode->i_blocks = 0;
  447. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  448. switch(mode & S_IFMT) {
  449. case S_IFDIR:
  450. inode->i_fop = &simple_dir_operations;
  451. inode->i_op = &simple_dir_inode_operations;
  452. inc_nlink(inode);
  453. default:
  454. break;
  455. }
  456. return inode;
  457. }
  458. /*
  459. * FIXME: This probably has races.
  460. */
  461. static void
  462. rpc_depopulate(struct dentry *parent, int start, int eof)
  463. {
  464. struct inode *dir = parent->d_inode;
  465. struct list_head *pos, *next;
  466. struct dentry *dentry, *dvec[10];
  467. int n = 0;
  468. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  469. repeat:
  470. spin_lock(&dcache_lock);
  471. list_for_each_safe(pos, next, &parent->d_subdirs) {
  472. dentry = list_entry(pos, struct dentry, d_u.d_child);
  473. if (!dentry->d_inode ||
  474. dentry->d_inode->i_ino < start ||
  475. dentry->d_inode->i_ino >= eof)
  476. continue;
  477. spin_lock(&dentry->d_lock);
  478. if (!d_unhashed(dentry)) {
  479. dget_locked(dentry);
  480. __d_drop(dentry);
  481. spin_unlock(&dentry->d_lock);
  482. dvec[n++] = dentry;
  483. if (n == ARRAY_SIZE(dvec))
  484. break;
  485. } else
  486. spin_unlock(&dentry->d_lock);
  487. }
  488. spin_unlock(&dcache_lock);
  489. if (n) {
  490. do {
  491. dentry = dvec[--n];
  492. if (S_ISREG(dentry->d_inode->i_mode))
  493. simple_unlink(dir, dentry);
  494. else if (S_ISDIR(dentry->d_inode->i_mode))
  495. simple_rmdir(dir, dentry);
  496. d_delete(dentry);
  497. dput(dentry);
  498. } while (n);
  499. goto repeat;
  500. }
  501. mutex_unlock(&dir->i_mutex);
  502. }
  503. static int
  504. rpc_populate(struct dentry *parent,
  505. struct rpc_filelist *files,
  506. int start, int eof)
  507. {
  508. struct inode *inode, *dir = parent->d_inode;
  509. void *private = RPC_I(dir)->private;
  510. struct dentry *dentry;
  511. int mode, i;
  512. mutex_lock(&dir->i_mutex);
  513. for (i = start; i < eof; i++) {
  514. dentry = d_alloc_name(parent, files[i].name);
  515. if (!dentry)
  516. goto out_bad;
  517. dentry->d_op = &rpc_dentry_operations;
  518. mode = files[i].mode;
  519. inode = rpc_get_inode(dir->i_sb, mode);
  520. if (!inode) {
  521. dput(dentry);
  522. goto out_bad;
  523. }
  524. inode->i_ino = i;
  525. if (files[i].i_fop)
  526. inode->i_fop = files[i].i_fop;
  527. if (private)
  528. rpc_inode_setowner(inode, private);
  529. if (S_ISDIR(mode))
  530. inc_nlink(dir);
  531. d_add(dentry, inode);
  532. }
  533. mutex_unlock(&dir->i_mutex);
  534. return 0;
  535. out_bad:
  536. mutex_unlock(&dir->i_mutex);
  537. printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
  538. __FILE__, __FUNCTION__, parent->d_name.name);
  539. return -ENOMEM;
  540. }
  541. static int
  542. __rpc_mkdir(struct inode *dir, struct dentry *dentry)
  543. {
  544. struct inode *inode;
  545. inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
  546. if (!inode)
  547. goto out_err;
  548. inode->i_ino = iunique(dir->i_sb, 100);
  549. d_instantiate(dentry, inode);
  550. inc_nlink(dir);
  551. inode_dir_notify(dir, DN_CREATE);
  552. return 0;
  553. out_err:
  554. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
  555. __FILE__, __FUNCTION__, dentry->d_name.name);
  556. return -ENOMEM;
  557. }
  558. static int
  559. __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  560. {
  561. int error;
  562. error = simple_rmdir(dir, dentry);
  563. if (!error)
  564. d_delete(dentry);
  565. return error;
  566. }
  567. static struct dentry *
  568. rpc_lookup_create(struct dentry *parent, const char *name, int len, int exclusive)
  569. {
  570. struct inode *dir = parent->d_inode;
  571. struct dentry *dentry;
  572. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  573. dentry = lookup_one_len(name, parent, len);
  574. if (IS_ERR(dentry))
  575. goto out_err;
  576. if (!dentry->d_inode)
  577. dentry->d_op = &rpc_dentry_operations;
  578. else if (exclusive) {
  579. dput(dentry);
  580. dentry = ERR_PTR(-EEXIST);
  581. goto out_err;
  582. }
  583. return dentry;
  584. out_err:
  585. mutex_unlock(&dir->i_mutex);
  586. return dentry;
  587. }
  588. static struct dentry *
  589. rpc_lookup_negative(char *path, struct nameidata *nd)
  590. {
  591. struct dentry *dentry;
  592. int error;
  593. if ((error = rpc_lookup_parent(path, nd)) != 0)
  594. return ERR_PTR(error);
  595. dentry = rpc_lookup_create(nd->dentry, nd->last.name, nd->last.len, 1);
  596. if (IS_ERR(dentry))
  597. rpc_release_path(nd);
  598. return dentry;
  599. }
  600. struct dentry *
  601. rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
  602. {
  603. struct nameidata nd;
  604. struct dentry *dentry;
  605. struct inode *dir;
  606. int error;
  607. dentry = rpc_lookup_negative(path, &nd);
  608. if (IS_ERR(dentry))
  609. return dentry;
  610. dir = nd.dentry->d_inode;
  611. if ((error = __rpc_mkdir(dir, dentry)) != 0)
  612. goto err_dput;
  613. RPC_I(dentry->d_inode)->private = rpc_client;
  614. error = rpc_populate(dentry, authfiles,
  615. RPCAUTH_info, RPCAUTH_EOF);
  616. if (error)
  617. goto err_depopulate;
  618. dget(dentry);
  619. out:
  620. mutex_unlock(&dir->i_mutex);
  621. rpc_release_path(&nd);
  622. return dentry;
  623. err_depopulate:
  624. rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
  625. __rpc_rmdir(dir, dentry);
  626. err_dput:
  627. dput(dentry);
  628. printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
  629. __FILE__, __FUNCTION__, path, error);
  630. dentry = ERR_PTR(error);
  631. goto out;
  632. }
  633. int
  634. rpc_rmdir(struct dentry *dentry)
  635. {
  636. struct dentry *parent;
  637. struct inode *dir;
  638. int error;
  639. parent = dget_parent(dentry);
  640. dir = parent->d_inode;
  641. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  642. rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
  643. error = __rpc_rmdir(dir, dentry);
  644. dput(dentry);
  645. mutex_unlock(&dir->i_mutex);
  646. dput(parent);
  647. return error;
  648. }
  649. struct dentry *
  650. rpc_mkpipe(struct dentry *parent, const char *name, void *private, struct rpc_pipe_ops *ops, int flags)
  651. {
  652. struct dentry *dentry;
  653. struct inode *dir, *inode;
  654. struct rpc_inode *rpci;
  655. dentry = rpc_lookup_create(parent, name, strlen(name), 0);
  656. if (IS_ERR(dentry))
  657. return dentry;
  658. dir = parent->d_inode;
  659. if (dentry->d_inode) {
  660. rpci = RPC_I(dentry->d_inode);
  661. if (rpci->private != private ||
  662. rpci->ops != ops ||
  663. rpci->flags != flags) {
  664. dput (dentry);
  665. dentry = ERR_PTR(-EBUSY);
  666. }
  667. rpci->nkern_readwriters++;
  668. goto out;
  669. }
  670. inode = rpc_get_inode(dir->i_sb, S_IFIFO | S_IRUSR | S_IWUSR);
  671. if (!inode)
  672. goto err_dput;
  673. inode->i_ino = iunique(dir->i_sb, 100);
  674. inode->i_fop = &rpc_pipe_fops;
  675. d_instantiate(dentry, inode);
  676. rpci = RPC_I(inode);
  677. rpci->private = private;
  678. rpci->flags = flags;
  679. rpci->ops = ops;
  680. rpci->nkern_readwriters = 1;
  681. inode_dir_notify(dir, DN_CREATE);
  682. dget(dentry);
  683. out:
  684. mutex_unlock(&dir->i_mutex);
  685. return dentry;
  686. err_dput:
  687. dput(dentry);
  688. dentry = ERR_PTR(-ENOMEM);
  689. printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
  690. __FILE__, __FUNCTION__, parent->d_name.name, name,
  691. -ENOMEM);
  692. goto out;
  693. }
  694. int
  695. rpc_unlink(struct dentry *dentry)
  696. {
  697. struct dentry *parent;
  698. struct inode *dir;
  699. int error = 0;
  700. parent = dget_parent(dentry);
  701. dir = parent->d_inode;
  702. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  703. if (--RPC_I(dentry->d_inode)->nkern_readwriters == 0) {
  704. rpc_close_pipes(dentry->d_inode);
  705. error = simple_unlink(dir, dentry);
  706. if (!error)
  707. d_delete(dentry);
  708. }
  709. dput(dentry);
  710. mutex_unlock(&dir->i_mutex);
  711. dput(parent);
  712. return error;
  713. }
  714. /*
  715. * populate the filesystem
  716. */
  717. static struct super_operations s_ops = {
  718. .alloc_inode = rpc_alloc_inode,
  719. .destroy_inode = rpc_destroy_inode,
  720. .statfs = simple_statfs,
  721. };
  722. #define RPCAUTH_GSSMAGIC 0x67596969
  723. static int
  724. rpc_fill_super(struct super_block *sb, void *data, int silent)
  725. {
  726. struct inode *inode;
  727. struct dentry *root;
  728. sb->s_blocksize = PAGE_CACHE_SIZE;
  729. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  730. sb->s_magic = RPCAUTH_GSSMAGIC;
  731. sb->s_op = &s_ops;
  732. sb->s_time_gran = 1;
  733. inode = rpc_get_inode(sb, S_IFDIR | 0755);
  734. if (!inode)
  735. return -ENOMEM;
  736. root = d_alloc_root(inode);
  737. if (!root) {
  738. iput(inode);
  739. return -ENOMEM;
  740. }
  741. if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
  742. goto out;
  743. sb->s_root = root;
  744. return 0;
  745. out:
  746. d_genocide(root);
  747. dput(root);
  748. return -ENOMEM;
  749. }
  750. static int
  751. rpc_get_sb(struct file_system_type *fs_type,
  752. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  753. {
  754. return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
  755. }
  756. static struct file_system_type rpc_pipe_fs_type = {
  757. .owner = THIS_MODULE,
  758. .name = "rpc_pipefs",
  759. .get_sb = rpc_get_sb,
  760. .kill_sb = kill_litter_super,
  761. };
  762. static void
  763. init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  764. {
  765. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  766. inode_init_once(&rpci->vfs_inode);
  767. rpci->private = NULL;
  768. rpci->nreaders = 0;
  769. rpci->nwriters = 0;
  770. INIT_LIST_HEAD(&rpci->in_upcall);
  771. INIT_LIST_HEAD(&rpci->in_downcall);
  772. INIT_LIST_HEAD(&rpci->pipe);
  773. rpci->pipelen = 0;
  774. init_waitqueue_head(&rpci->waitq);
  775. INIT_DELAYED_WORK(&rpci->queue_timeout,
  776. rpc_timeout_upcall_queue);
  777. rpci->ops = NULL;
  778. }
  779. int register_rpc_pipefs(void)
  780. {
  781. int err;
  782. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  783. sizeof(struct rpc_inode),
  784. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  785. SLAB_MEM_SPREAD),
  786. init_once, NULL);
  787. if (!rpc_inode_cachep)
  788. return -ENOMEM;
  789. err = register_filesystem(&rpc_pipe_fs_type);
  790. if (err) {
  791. kmem_cache_destroy(rpc_inode_cachep);
  792. return err;
  793. }
  794. return 0;
  795. }
  796. void unregister_rpc_pipefs(void)
  797. {
  798. kmem_cache_destroy(rpc_inode_cachep);
  799. unregister_filesystem(&rpc_pipe_fs_type);
  800. }