rpc_pipe.c 19 KB

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