rpc_pipe.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 <linux/rcupdate.h>
  20. #include <asm/ioctls.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/nsproxy.h>
  29. #include <linux/notifier.h>
  30. #include "netns.h"
  31. #include "sunrpc.h"
  32. #define RPCDBG_FACILITY RPCDBG_DEBUG
  33. #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
  34. static struct file_system_type rpc_pipe_fs_type;
  35. static struct kmem_cache *rpc_inode_cachep __read_mostly;
  36. #define RPC_UPCALL_TIMEOUT (30*HZ)
  37. static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
  38. int rpc_pipefs_notifier_register(struct notifier_block *nb)
  39. {
  40. return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
  41. }
  42. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
  43. void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
  44. {
  45. blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
  46. }
  47. EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
  48. static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
  49. void (*destroy_msg)(struct rpc_pipe_msg *), int err)
  50. {
  51. struct rpc_pipe_msg *msg;
  52. if (list_empty(head))
  53. return;
  54. do {
  55. msg = list_entry(head->next, struct rpc_pipe_msg, list);
  56. list_del_init(&msg->list);
  57. msg->errno = err;
  58. destroy_msg(msg);
  59. } while (!list_empty(head));
  60. if (waitq)
  61. wake_up(waitq);
  62. }
  63. static void
  64. rpc_timeout_upcall_queue(struct work_struct *work)
  65. {
  66. LIST_HEAD(free_list);
  67. struct rpc_pipe *pipe =
  68. container_of(work, struct rpc_pipe, queue_timeout.work);
  69. void (*destroy_msg)(struct rpc_pipe_msg *);
  70. struct dentry *dentry;
  71. spin_lock(&pipe->lock);
  72. destroy_msg = pipe->ops->destroy_msg;
  73. if (pipe->nreaders == 0) {
  74. list_splice_init(&pipe->pipe, &free_list);
  75. pipe->pipelen = 0;
  76. }
  77. dentry = dget(pipe->dentry);
  78. spin_unlock(&pipe->lock);
  79. rpc_purge_list(dentry ? &RPC_I(dentry->d_inode)->waitq : NULL,
  80. &free_list, destroy_msg, -ETIMEDOUT);
  81. dput(dentry);
  82. }
  83. ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
  84. char __user *dst, size_t buflen)
  85. {
  86. char *data = (char *)msg->data + msg->copied;
  87. size_t mlen = min(msg->len - msg->copied, buflen);
  88. unsigned long left;
  89. left = copy_to_user(dst, data, mlen);
  90. if (left == mlen) {
  91. msg->errno = -EFAULT;
  92. return -EFAULT;
  93. }
  94. mlen -= left;
  95. msg->copied += mlen;
  96. msg->errno = 0;
  97. return mlen;
  98. }
  99. EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
  100. /**
  101. * rpc_queue_upcall - queue an upcall message to userspace
  102. * @pipe: upcall pipe on which to queue given message
  103. * @msg: message to queue
  104. *
  105. * Call with an @inode created by rpc_mkpipe() to queue an upcall.
  106. * A userspace process may then later read the upcall by performing a
  107. * read on an open file for this inode. It is up to the caller to
  108. * initialize the fields of @msg (other than @msg->list) appropriately.
  109. */
  110. int
  111. rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
  112. {
  113. int res = -EPIPE;
  114. struct dentry *dentry;
  115. spin_lock(&pipe->lock);
  116. if (pipe->nreaders) {
  117. list_add_tail(&msg->list, &pipe->pipe);
  118. pipe->pipelen += msg->len;
  119. res = 0;
  120. } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
  121. if (list_empty(&pipe->pipe))
  122. queue_delayed_work(rpciod_workqueue,
  123. &pipe->queue_timeout,
  124. RPC_UPCALL_TIMEOUT);
  125. list_add_tail(&msg->list, &pipe->pipe);
  126. pipe->pipelen += msg->len;
  127. res = 0;
  128. }
  129. dentry = dget(pipe->dentry);
  130. spin_unlock(&pipe->lock);
  131. if (dentry) {
  132. wake_up(&RPC_I(dentry->d_inode)->waitq);
  133. dput(dentry);
  134. }
  135. return res;
  136. }
  137. EXPORT_SYMBOL_GPL(rpc_queue_upcall);
  138. static inline void
  139. rpc_inode_setowner(struct inode *inode, void *private)
  140. {
  141. RPC_I(inode)->private = private;
  142. }
  143. static void
  144. rpc_close_pipes(struct inode *inode)
  145. {
  146. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  147. int need_release;
  148. LIST_HEAD(free_list);
  149. mutex_lock(&inode->i_mutex);
  150. spin_lock(&pipe->lock);
  151. need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
  152. pipe->nreaders = 0;
  153. list_splice_init(&pipe->in_upcall, &free_list);
  154. list_splice_init(&pipe->pipe, &free_list);
  155. pipe->pipelen = 0;
  156. pipe->dentry = NULL;
  157. spin_unlock(&pipe->lock);
  158. rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
  159. pipe->nwriters = 0;
  160. if (need_release && pipe->ops->release_pipe)
  161. pipe->ops->release_pipe(inode);
  162. cancel_delayed_work_sync(&pipe->queue_timeout);
  163. rpc_inode_setowner(inode, NULL);
  164. RPC_I(inode)->pipe = NULL;
  165. mutex_unlock(&inode->i_mutex);
  166. }
  167. static struct inode *
  168. rpc_alloc_inode(struct super_block *sb)
  169. {
  170. struct rpc_inode *rpci;
  171. rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
  172. if (!rpci)
  173. return NULL;
  174. return &rpci->vfs_inode;
  175. }
  176. static void
  177. rpc_i_callback(struct rcu_head *head)
  178. {
  179. struct inode *inode = container_of(head, struct inode, i_rcu);
  180. kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
  181. }
  182. static void
  183. rpc_destroy_inode(struct inode *inode)
  184. {
  185. call_rcu(&inode->i_rcu, rpc_i_callback);
  186. }
  187. static int
  188. rpc_pipe_open(struct inode *inode, struct file *filp)
  189. {
  190. struct net *net = inode->i_sb->s_fs_info;
  191. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  192. struct rpc_pipe *pipe;
  193. int first_open;
  194. int res = -ENXIO;
  195. mutex_lock(&inode->i_mutex);
  196. sn->gssd_running = 1;
  197. pipe = RPC_I(inode)->pipe;
  198. if (pipe == NULL)
  199. goto out;
  200. first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
  201. if (first_open && pipe->ops->open_pipe) {
  202. res = pipe->ops->open_pipe(inode);
  203. if (res)
  204. goto out;
  205. }
  206. if (filp->f_mode & FMODE_READ)
  207. pipe->nreaders++;
  208. if (filp->f_mode & FMODE_WRITE)
  209. pipe->nwriters++;
  210. res = 0;
  211. out:
  212. mutex_unlock(&inode->i_mutex);
  213. return res;
  214. }
  215. static int
  216. rpc_pipe_release(struct inode *inode, struct file *filp)
  217. {
  218. struct rpc_pipe *pipe;
  219. struct rpc_pipe_msg *msg;
  220. int last_close;
  221. mutex_lock(&inode->i_mutex);
  222. pipe = RPC_I(inode)->pipe;
  223. if (pipe == NULL)
  224. goto out;
  225. msg = filp->private_data;
  226. if (msg != NULL) {
  227. spin_lock(&pipe->lock);
  228. msg->errno = -EAGAIN;
  229. list_del_init(&msg->list);
  230. spin_unlock(&pipe->lock);
  231. pipe->ops->destroy_msg(msg);
  232. }
  233. if (filp->f_mode & FMODE_WRITE)
  234. pipe->nwriters --;
  235. if (filp->f_mode & FMODE_READ) {
  236. pipe->nreaders --;
  237. if (pipe->nreaders == 0) {
  238. LIST_HEAD(free_list);
  239. spin_lock(&pipe->lock);
  240. list_splice_init(&pipe->pipe, &free_list);
  241. pipe->pipelen = 0;
  242. spin_unlock(&pipe->lock);
  243. rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
  244. pipe->ops->destroy_msg, -EAGAIN);
  245. }
  246. }
  247. last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
  248. if (last_close && pipe->ops->release_pipe)
  249. pipe->ops->release_pipe(inode);
  250. out:
  251. mutex_unlock(&inode->i_mutex);
  252. return 0;
  253. }
  254. static ssize_t
  255. rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
  256. {
  257. struct inode *inode = file_inode(filp);
  258. struct rpc_pipe *pipe;
  259. struct rpc_pipe_msg *msg;
  260. int res = 0;
  261. mutex_lock(&inode->i_mutex);
  262. pipe = RPC_I(inode)->pipe;
  263. if (pipe == NULL) {
  264. res = -EPIPE;
  265. goto out_unlock;
  266. }
  267. msg = filp->private_data;
  268. if (msg == NULL) {
  269. spin_lock(&pipe->lock);
  270. if (!list_empty(&pipe->pipe)) {
  271. msg = list_entry(pipe->pipe.next,
  272. struct rpc_pipe_msg,
  273. list);
  274. list_move(&msg->list, &pipe->in_upcall);
  275. pipe->pipelen -= msg->len;
  276. filp->private_data = msg;
  277. msg->copied = 0;
  278. }
  279. spin_unlock(&pipe->lock);
  280. if (msg == NULL)
  281. goto out_unlock;
  282. }
  283. /* NOTE: it is up to the callback to update msg->copied */
  284. res = pipe->ops->upcall(filp, msg, buf, len);
  285. if (res < 0 || msg->len == msg->copied) {
  286. filp->private_data = NULL;
  287. spin_lock(&pipe->lock);
  288. list_del_init(&msg->list);
  289. spin_unlock(&pipe->lock);
  290. pipe->ops->destroy_msg(msg);
  291. }
  292. out_unlock:
  293. mutex_unlock(&inode->i_mutex);
  294. return res;
  295. }
  296. static ssize_t
  297. rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
  298. {
  299. struct inode *inode = file_inode(filp);
  300. int res;
  301. mutex_lock(&inode->i_mutex);
  302. res = -EPIPE;
  303. if (RPC_I(inode)->pipe != NULL)
  304. res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
  305. mutex_unlock(&inode->i_mutex);
  306. return res;
  307. }
  308. static unsigned int
  309. rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
  310. {
  311. struct inode *inode = file_inode(filp);
  312. struct rpc_inode *rpci = RPC_I(inode);
  313. unsigned int mask = POLLOUT | POLLWRNORM;
  314. poll_wait(filp, &rpci->waitq, wait);
  315. mutex_lock(&inode->i_mutex);
  316. if (rpci->pipe == NULL)
  317. mask |= POLLERR | POLLHUP;
  318. else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
  319. mask |= POLLIN | POLLRDNORM;
  320. mutex_unlock(&inode->i_mutex);
  321. return mask;
  322. }
  323. static long
  324. rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  325. {
  326. struct inode *inode = file_inode(filp);
  327. struct rpc_pipe *pipe;
  328. int len;
  329. switch (cmd) {
  330. case FIONREAD:
  331. mutex_lock(&inode->i_mutex);
  332. pipe = RPC_I(inode)->pipe;
  333. if (pipe == NULL) {
  334. mutex_unlock(&inode->i_mutex);
  335. return -EPIPE;
  336. }
  337. spin_lock(&pipe->lock);
  338. len = pipe->pipelen;
  339. if (filp->private_data) {
  340. struct rpc_pipe_msg *msg;
  341. msg = filp->private_data;
  342. len += msg->len - msg->copied;
  343. }
  344. spin_unlock(&pipe->lock);
  345. mutex_unlock(&inode->i_mutex);
  346. return put_user(len, (int __user *)arg);
  347. default:
  348. return -EINVAL;
  349. }
  350. }
  351. static const struct file_operations rpc_pipe_fops = {
  352. .owner = THIS_MODULE,
  353. .llseek = no_llseek,
  354. .read = rpc_pipe_read,
  355. .write = rpc_pipe_write,
  356. .poll = rpc_pipe_poll,
  357. .unlocked_ioctl = rpc_pipe_ioctl,
  358. .open = rpc_pipe_open,
  359. .release = rpc_pipe_release,
  360. };
  361. static int
  362. rpc_show_info(struct seq_file *m, void *v)
  363. {
  364. struct rpc_clnt *clnt = m->private;
  365. rcu_read_lock();
  366. seq_printf(m, "RPC server: %s\n",
  367. rcu_dereference(clnt->cl_xprt)->servername);
  368. seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
  369. clnt->cl_prog, clnt->cl_vers);
  370. seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
  371. seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
  372. seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
  373. rcu_read_unlock();
  374. return 0;
  375. }
  376. static int
  377. rpc_info_open(struct inode *inode, struct file *file)
  378. {
  379. struct rpc_clnt *clnt = NULL;
  380. int ret = single_open(file, rpc_show_info, NULL);
  381. if (!ret) {
  382. struct seq_file *m = file->private_data;
  383. spin_lock(&file->f_path.dentry->d_lock);
  384. if (!d_unhashed(file->f_path.dentry))
  385. clnt = RPC_I(inode)->private;
  386. if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
  387. spin_unlock(&file->f_path.dentry->d_lock);
  388. m->private = clnt;
  389. } else {
  390. spin_unlock(&file->f_path.dentry->d_lock);
  391. single_release(inode, file);
  392. ret = -EINVAL;
  393. }
  394. }
  395. return ret;
  396. }
  397. static int
  398. rpc_info_release(struct inode *inode, struct file *file)
  399. {
  400. struct seq_file *m = file->private_data;
  401. struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
  402. if (clnt)
  403. rpc_release_client(clnt);
  404. return single_release(inode, file);
  405. }
  406. static const struct file_operations rpc_info_operations = {
  407. .owner = THIS_MODULE,
  408. .open = rpc_info_open,
  409. .read = seq_read,
  410. .llseek = seq_lseek,
  411. .release = rpc_info_release,
  412. };
  413. /*
  414. * Description of fs contents.
  415. */
  416. struct rpc_filelist {
  417. const char *name;
  418. const struct file_operations *i_fop;
  419. umode_t mode;
  420. };
  421. static struct inode *
  422. rpc_get_inode(struct super_block *sb, umode_t mode)
  423. {
  424. struct inode *inode = new_inode(sb);
  425. if (!inode)
  426. return NULL;
  427. inode->i_ino = get_next_ino();
  428. inode->i_mode = mode;
  429. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  430. switch (mode & S_IFMT) {
  431. case S_IFDIR:
  432. inode->i_fop = &simple_dir_operations;
  433. inode->i_op = &simple_dir_inode_operations;
  434. inc_nlink(inode);
  435. default:
  436. break;
  437. }
  438. return inode;
  439. }
  440. static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
  441. umode_t mode,
  442. const struct file_operations *i_fop,
  443. void *private)
  444. {
  445. struct inode *inode;
  446. d_drop(dentry);
  447. inode = rpc_get_inode(dir->i_sb, mode);
  448. if (!inode)
  449. goto out_err;
  450. inode->i_ino = iunique(dir->i_sb, 100);
  451. if (i_fop)
  452. inode->i_fop = i_fop;
  453. if (private)
  454. rpc_inode_setowner(inode, private);
  455. d_add(dentry, inode);
  456. return 0;
  457. out_err:
  458. printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
  459. __FILE__, __func__, dentry);
  460. dput(dentry);
  461. return -ENOMEM;
  462. }
  463. static int __rpc_create(struct inode *dir, struct dentry *dentry,
  464. umode_t mode,
  465. const struct file_operations *i_fop,
  466. void *private)
  467. {
  468. int err;
  469. err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
  470. if (err)
  471. return err;
  472. fsnotify_create(dir, dentry);
  473. return 0;
  474. }
  475. static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
  476. umode_t mode,
  477. const struct file_operations *i_fop,
  478. void *private)
  479. {
  480. int err;
  481. err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
  482. if (err)
  483. return err;
  484. inc_nlink(dir);
  485. fsnotify_mkdir(dir, dentry);
  486. return 0;
  487. }
  488. static void
  489. init_pipe(struct rpc_pipe *pipe)
  490. {
  491. pipe->nreaders = 0;
  492. pipe->nwriters = 0;
  493. INIT_LIST_HEAD(&pipe->in_upcall);
  494. INIT_LIST_HEAD(&pipe->in_downcall);
  495. INIT_LIST_HEAD(&pipe->pipe);
  496. pipe->pipelen = 0;
  497. INIT_DELAYED_WORK(&pipe->queue_timeout,
  498. rpc_timeout_upcall_queue);
  499. pipe->ops = NULL;
  500. spin_lock_init(&pipe->lock);
  501. pipe->dentry = NULL;
  502. }
  503. void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
  504. {
  505. kfree(pipe);
  506. }
  507. EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
  508. struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
  509. {
  510. struct rpc_pipe *pipe;
  511. pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
  512. if (!pipe)
  513. return ERR_PTR(-ENOMEM);
  514. init_pipe(pipe);
  515. pipe->ops = ops;
  516. pipe->flags = flags;
  517. return pipe;
  518. }
  519. EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
  520. static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
  521. umode_t mode,
  522. const struct file_operations *i_fop,
  523. void *private,
  524. struct rpc_pipe *pipe)
  525. {
  526. struct rpc_inode *rpci;
  527. int err;
  528. err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
  529. if (err)
  530. return err;
  531. rpci = RPC_I(dentry->d_inode);
  532. rpci->private = private;
  533. rpci->pipe = pipe;
  534. fsnotify_create(dir, dentry);
  535. return 0;
  536. }
  537. static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
  538. {
  539. int ret;
  540. dget(dentry);
  541. ret = simple_rmdir(dir, dentry);
  542. d_delete(dentry);
  543. dput(dentry);
  544. return ret;
  545. }
  546. int rpc_rmdir(struct dentry *dentry)
  547. {
  548. struct dentry *parent;
  549. struct inode *dir;
  550. int error;
  551. parent = dget_parent(dentry);
  552. dir = parent->d_inode;
  553. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  554. error = __rpc_rmdir(dir, dentry);
  555. mutex_unlock(&dir->i_mutex);
  556. dput(parent);
  557. return error;
  558. }
  559. EXPORT_SYMBOL_GPL(rpc_rmdir);
  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. rpc_close_pipes(inode);
  573. return __rpc_unlink(dir, dentry);
  574. }
  575. static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
  576. const char *name)
  577. {
  578. struct qstr q = QSTR_INIT(name, strlen(name));
  579. struct dentry *dentry = d_hash_and_lookup(parent, &q);
  580. if (!dentry) {
  581. dentry = d_alloc(parent, &q);
  582. if (!dentry)
  583. return ERR_PTR(-ENOMEM);
  584. }
  585. if (dentry->d_inode == NULL)
  586. return dentry;
  587. dput(dentry);
  588. return ERR_PTR(-EEXIST);
  589. }
  590. /*
  591. * FIXME: This probably has races.
  592. */
  593. static void __rpc_depopulate(struct dentry *parent,
  594. const struct rpc_filelist *files,
  595. int start, int eof)
  596. {
  597. struct inode *dir = parent->d_inode;
  598. struct dentry *dentry;
  599. struct qstr name;
  600. int i;
  601. for (i = start; i < eof; i++) {
  602. name.name = files[i].name;
  603. name.len = strlen(files[i].name);
  604. dentry = d_hash_and_lookup(parent, &name);
  605. if (dentry == NULL)
  606. continue;
  607. if (dentry->d_inode == NULL)
  608. goto next;
  609. switch (dentry->d_inode->i_mode & S_IFMT) {
  610. default:
  611. BUG();
  612. case S_IFREG:
  613. __rpc_unlink(dir, dentry);
  614. break;
  615. case S_IFDIR:
  616. __rpc_rmdir(dir, dentry);
  617. }
  618. next:
  619. dput(dentry);
  620. }
  621. }
  622. static void rpc_depopulate(struct dentry *parent,
  623. const struct rpc_filelist *files,
  624. int start, int eof)
  625. {
  626. struct inode *dir = parent->d_inode;
  627. mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
  628. __rpc_depopulate(parent, files, start, eof);
  629. mutex_unlock(&dir->i_mutex);
  630. }
  631. static int rpc_populate(struct dentry *parent,
  632. const struct rpc_filelist *files,
  633. int start, int eof,
  634. void *private)
  635. {
  636. struct inode *dir = parent->d_inode;
  637. struct dentry *dentry;
  638. int i, err;
  639. mutex_lock(&dir->i_mutex);
  640. for (i = start; i < eof; i++) {
  641. dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
  642. err = PTR_ERR(dentry);
  643. if (IS_ERR(dentry))
  644. goto out_bad;
  645. switch (files[i].mode & S_IFMT) {
  646. default:
  647. BUG();
  648. case S_IFREG:
  649. err = __rpc_create(dir, dentry,
  650. files[i].mode,
  651. files[i].i_fop,
  652. private);
  653. break;
  654. case S_IFDIR:
  655. err = __rpc_mkdir(dir, dentry,
  656. files[i].mode,
  657. NULL,
  658. private);
  659. }
  660. if (err != 0)
  661. goto out_bad;
  662. }
  663. mutex_unlock(&dir->i_mutex);
  664. return 0;
  665. out_bad:
  666. __rpc_depopulate(parent, files, start, eof);
  667. mutex_unlock(&dir->i_mutex);
  668. printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
  669. __FILE__, __func__, parent);
  670. return err;
  671. }
  672. static struct dentry *rpc_mkdir_populate(struct dentry *parent,
  673. const char *name, umode_t mode, void *private,
  674. int (*populate)(struct dentry *, void *), void *args_populate)
  675. {
  676. struct dentry *dentry;
  677. struct inode *dir = parent->d_inode;
  678. int error;
  679. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  680. dentry = __rpc_lookup_create_exclusive(parent, name);
  681. if (IS_ERR(dentry))
  682. goto out;
  683. error = __rpc_mkdir(dir, dentry, mode, NULL, private);
  684. if (error != 0)
  685. goto out_err;
  686. if (populate != NULL) {
  687. error = populate(dentry, args_populate);
  688. if (error)
  689. goto err_rmdir;
  690. }
  691. out:
  692. mutex_unlock(&dir->i_mutex);
  693. return dentry;
  694. err_rmdir:
  695. __rpc_rmdir(dir, dentry);
  696. out_err:
  697. dentry = ERR_PTR(error);
  698. goto out;
  699. }
  700. static int rpc_rmdir_depopulate(struct dentry *dentry,
  701. void (*depopulate)(struct dentry *))
  702. {
  703. struct dentry *parent;
  704. struct inode *dir;
  705. int error;
  706. parent = dget_parent(dentry);
  707. dir = parent->d_inode;
  708. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  709. if (depopulate != NULL)
  710. depopulate(dentry);
  711. error = __rpc_rmdir(dir, dentry);
  712. mutex_unlock(&dir->i_mutex);
  713. dput(parent);
  714. return error;
  715. }
  716. /**
  717. * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
  718. * @parent: dentry of directory to create new "pipe" in
  719. * @name: name of pipe
  720. * @private: private data to associate with the pipe, for the caller's use
  721. * @pipe: &rpc_pipe containing input parameters
  722. *
  723. * Data is made available for userspace to read by calls to
  724. * rpc_queue_upcall(). The actual reads will result in calls to
  725. * @ops->upcall, which will be called with the file pointer,
  726. * message, and userspace buffer to copy to.
  727. *
  728. * Writes can come at any time, and do not necessarily have to be
  729. * responses to upcalls. They will result in calls to @msg->downcall.
  730. *
  731. * The @private argument passed here will be available to all these methods
  732. * from the file pointer, via RPC_I(file_inode(file))->private.
  733. */
  734. struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
  735. void *private, struct rpc_pipe *pipe)
  736. {
  737. struct dentry *dentry;
  738. struct inode *dir = parent->d_inode;
  739. umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
  740. int err;
  741. if (pipe->ops->upcall == NULL)
  742. umode &= ~S_IRUGO;
  743. if (pipe->ops->downcall == NULL)
  744. umode &= ~S_IWUGO;
  745. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  746. dentry = __rpc_lookup_create_exclusive(parent, name);
  747. if (IS_ERR(dentry))
  748. goto out;
  749. err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
  750. private, pipe);
  751. if (err)
  752. goto out_err;
  753. out:
  754. mutex_unlock(&dir->i_mutex);
  755. return dentry;
  756. out_err:
  757. dentry = ERR_PTR(err);
  758. printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
  759. __FILE__, __func__, parent, name,
  760. err);
  761. goto out;
  762. }
  763. EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
  764. /**
  765. * rpc_unlink - remove a pipe
  766. * @dentry: dentry for the pipe, as returned from rpc_mkpipe
  767. *
  768. * After this call, lookups will no longer find the pipe, and any
  769. * attempts to read or write using preexisting opens of the pipe will
  770. * return -EPIPE.
  771. */
  772. int
  773. rpc_unlink(struct dentry *dentry)
  774. {
  775. struct dentry *parent;
  776. struct inode *dir;
  777. int error = 0;
  778. parent = dget_parent(dentry);
  779. dir = parent->d_inode;
  780. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  781. error = __rpc_rmpipe(dir, dentry);
  782. mutex_unlock(&dir->i_mutex);
  783. dput(parent);
  784. return error;
  785. }
  786. EXPORT_SYMBOL_GPL(rpc_unlink);
  787. /**
  788. * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
  789. * @pdh: pointer to struct rpc_pipe_dir_head
  790. */
  791. void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
  792. {
  793. INIT_LIST_HEAD(&pdh->pdh_entries);
  794. pdh->pdh_dentry = NULL;
  795. }
  796. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
  797. /**
  798. * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
  799. * @pdo: pointer to struct rpc_pipe_dir_object
  800. * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
  801. * @pdo_data: pointer to caller-defined data
  802. */
  803. void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
  804. const struct rpc_pipe_dir_object_ops *pdo_ops,
  805. void *pdo_data)
  806. {
  807. INIT_LIST_HEAD(&pdo->pdo_head);
  808. pdo->pdo_ops = pdo_ops;
  809. pdo->pdo_data = pdo_data;
  810. }
  811. EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
  812. static int
  813. rpc_add_pipe_dir_object_locked(struct net *net,
  814. struct rpc_pipe_dir_head *pdh,
  815. struct rpc_pipe_dir_object *pdo)
  816. {
  817. int ret = 0;
  818. if (pdh->pdh_dentry)
  819. ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
  820. if (ret == 0)
  821. list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
  822. return ret;
  823. }
  824. static void
  825. rpc_remove_pipe_dir_object_locked(struct net *net,
  826. struct rpc_pipe_dir_head *pdh,
  827. struct rpc_pipe_dir_object *pdo)
  828. {
  829. if (pdh->pdh_dentry)
  830. pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
  831. list_del_init(&pdo->pdo_head);
  832. }
  833. /**
  834. * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
  835. * @net: pointer to struct net
  836. * @pdh: pointer to struct rpc_pipe_dir_head
  837. * @pdo: pointer to struct rpc_pipe_dir_object
  838. *
  839. */
  840. int
  841. rpc_add_pipe_dir_object(struct net *net,
  842. struct rpc_pipe_dir_head *pdh,
  843. struct rpc_pipe_dir_object *pdo)
  844. {
  845. int ret = 0;
  846. if (list_empty(&pdo->pdo_head)) {
  847. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  848. mutex_lock(&sn->pipefs_sb_lock);
  849. ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  850. mutex_unlock(&sn->pipefs_sb_lock);
  851. }
  852. return ret;
  853. }
  854. EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
  855. /**
  856. * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
  857. * @net: pointer to struct net
  858. * @pdh: pointer to struct rpc_pipe_dir_head
  859. * @pdo: pointer to struct rpc_pipe_dir_object
  860. *
  861. */
  862. void
  863. rpc_remove_pipe_dir_object(struct net *net,
  864. struct rpc_pipe_dir_head *pdh,
  865. struct rpc_pipe_dir_object *pdo)
  866. {
  867. if (!list_empty(&pdo->pdo_head)) {
  868. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  869. mutex_lock(&sn->pipefs_sb_lock);
  870. rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
  871. mutex_unlock(&sn->pipefs_sb_lock);
  872. }
  873. }
  874. EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
  875. /**
  876. * rpc_find_or_alloc_pipe_dir_object
  877. * @net: pointer to struct net
  878. * @pdh: pointer to struct rpc_pipe_dir_head
  879. * @match: match struct rpc_pipe_dir_object to data
  880. * @alloc: allocate a new struct rpc_pipe_dir_object
  881. * @data: user defined data for match() and alloc()
  882. *
  883. */
  884. struct rpc_pipe_dir_object *
  885. rpc_find_or_alloc_pipe_dir_object(struct net *net,
  886. struct rpc_pipe_dir_head *pdh,
  887. int (*match)(struct rpc_pipe_dir_object *, void *),
  888. struct rpc_pipe_dir_object *(*alloc)(void *),
  889. void *data)
  890. {
  891. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  892. struct rpc_pipe_dir_object *pdo;
  893. mutex_lock(&sn->pipefs_sb_lock);
  894. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
  895. if (!match(pdo, data))
  896. continue;
  897. goto out;
  898. }
  899. pdo = alloc(data);
  900. if (!pdo)
  901. goto out;
  902. rpc_add_pipe_dir_object_locked(net, pdh, pdo);
  903. out:
  904. mutex_unlock(&sn->pipefs_sb_lock);
  905. return pdo;
  906. }
  907. EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
  908. static void
  909. rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  910. {
  911. struct rpc_pipe_dir_object *pdo;
  912. struct dentry *dir = pdh->pdh_dentry;
  913. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  914. pdo->pdo_ops->create(dir, pdo);
  915. }
  916. static void
  917. rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
  918. {
  919. struct rpc_pipe_dir_object *pdo;
  920. struct dentry *dir = pdh->pdh_dentry;
  921. list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
  922. pdo->pdo_ops->destroy(dir, pdo);
  923. }
  924. enum {
  925. RPCAUTH_info,
  926. RPCAUTH_EOF
  927. };
  928. static const struct rpc_filelist authfiles[] = {
  929. [RPCAUTH_info] = {
  930. .name = "info",
  931. .i_fop = &rpc_info_operations,
  932. .mode = S_IFREG | S_IRUSR,
  933. },
  934. };
  935. static int rpc_clntdir_populate(struct dentry *dentry, void *private)
  936. {
  937. return rpc_populate(dentry,
  938. authfiles, RPCAUTH_info, RPCAUTH_EOF,
  939. private);
  940. }
  941. static void rpc_clntdir_depopulate(struct dentry *dentry)
  942. {
  943. rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
  944. }
  945. /**
  946. * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
  947. * @dentry: the parent of new directory
  948. * @name: the name of new directory
  949. * @rpc_client: rpc client to associate with this directory
  950. *
  951. * This creates a directory at the given @path associated with
  952. * @rpc_clnt, which will contain a file named "info" with some basic
  953. * information about the client, together with any "pipes" that may
  954. * later be created using rpc_mkpipe().
  955. */
  956. struct dentry *rpc_create_client_dir(struct dentry *dentry,
  957. const char *name,
  958. struct rpc_clnt *rpc_client)
  959. {
  960. struct dentry *ret;
  961. ret = rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
  962. rpc_clntdir_populate, rpc_client);
  963. if (!IS_ERR(ret)) {
  964. rpc_client->cl_pipedir_objects.pdh_dentry = ret;
  965. rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  966. }
  967. return ret;
  968. }
  969. /**
  970. * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
  971. * @rpc_client: rpc_client for the pipe
  972. */
  973. int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
  974. {
  975. struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
  976. if (dentry == NULL)
  977. return 0;
  978. rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
  979. rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
  980. return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
  981. }
  982. static const struct rpc_filelist cache_pipefs_files[3] = {
  983. [0] = {
  984. .name = "channel",
  985. .i_fop = &cache_file_operations_pipefs,
  986. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  987. },
  988. [1] = {
  989. .name = "content",
  990. .i_fop = &content_file_operations_pipefs,
  991. .mode = S_IFREG|S_IRUSR,
  992. },
  993. [2] = {
  994. .name = "flush",
  995. .i_fop = &cache_flush_operations_pipefs,
  996. .mode = S_IFREG|S_IRUSR|S_IWUSR,
  997. },
  998. };
  999. static int rpc_cachedir_populate(struct dentry *dentry, void *private)
  1000. {
  1001. return rpc_populate(dentry,
  1002. cache_pipefs_files, 0, 3,
  1003. private);
  1004. }
  1005. static void rpc_cachedir_depopulate(struct dentry *dentry)
  1006. {
  1007. rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
  1008. }
  1009. struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
  1010. umode_t umode, struct cache_detail *cd)
  1011. {
  1012. return rpc_mkdir_populate(parent, name, umode, NULL,
  1013. rpc_cachedir_populate, cd);
  1014. }
  1015. void rpc_remove_cache_dir(struct dentry *dentry)
  1016. {
  1017. rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
  1018. }
  1019. /*
  1020. * populate the filesystem
  1021. */
  1022. static const struct super_operations s_ops = {
  1023. .alloc_inode = rpc_alloc_inode,
  1024. .destroy_inode = rpc_destroy_inode,
  1025. .statfs = simple_statfs,
  1026. };
  1027. #define RPCAUTH_GSSMAGIC 0x67596969
  1028. /*
  1029. * We have a single directory with 1 node in it.
  1030. */
  1031. enum {
  1032. RPCAUTH_lockd,
  1033. RPCAUTH_mount,
  1034. RPCAUTH_nfs,
  1035. RPCAUTH_portmap,
  1036. RPCAUTH_statd,
  1037. RPCAUTH_nfsd4_cb,
  1038. RPCAUTH_cache,
  1039. RPCAUTH_nfsd,
  1040. RPCAUTH_RootEOF
  1041. };
  1042. static const struct rpc_filelist files[] = {
  1043. [RPCAUTH_lockd] = {
  1044. .name = "lockd",
  1045. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1046. },
  1047. [RPCAUTH_mount] = {
  1048. .name = "mount",
  1049. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1050. },
  1051. [RPCAUTH_nfs] = {
  1052. .name = "nfs",
  1053. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1054. },
  1055. [RPCAUTH_portmap] = {
  1056. .name = "portmap",
  1057. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1058. },
  1059. [RPCAUTH_statd] = {
  1060. .name = "statd",
  1061. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1062. },
  1063. [RPCAUTH_nfsd4_cb] = {
  1064. .name = "nfsd4_cb",
  1065. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1066. },
  1067. [RPCAUTH_cache] = {
  1068. .name = "cache",
  1069. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1070. },
  1071. [RPCAUTH_nfsd] = {
  1072. .name = "nfsd",
  1073. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  1074. },
  1075. };
  1076. /*
  1077. * This call can be used only in RPC pipefs mount notification hooks.
  1078. */
  1079. struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
  1080. const unsigned char *dir_name)
  1081. {
  1082. struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
  1083. return d_hash_and_lookup(sb->s_root, &dir);
  1084. }
  1085. EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
  1086. void rpc_pipefs_init_net(struct net *net)
  1087. {
  1088. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1089. mutex_init(&sn->pipefs_sb_lock);
  1090. sn->gssd_running = 1;
  1091. sn->pipe_version = -1;
  1092. }
  1093. /*
  1094. * This call will be used for per network namespace operations calls.
  1095. * Note: Function will be returned with pipefs_sb_lock taken if superblock was
  1096. * found. This lock have to be released by rpc_put_sb_net() when all operations
  1097. * will be completed.
  1098. */
  1099. struct super_block *rpc_get_sb_net(const struct net *net)
  1100. {
  1101. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1102. mutex_lock(&sn->pipefs_sb_lock);
  1103. if (sn->pipefs_sb)
  1104. return sn->pipefs_sb;
  1105. mutex_unlock(&sn->pipefs_sb_lock);
  1106. return NULL;
  1107. }
  1108. EXPORT_SYMBOL_GPL(rpc_get_sb_net);
  1109. void rpc_put_sb_net(const struct net *net)
  1110. {
  1111. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1112. WARN_ON(sn->pipefs_sb == NULL);
  1113. mutex_unlock(&sn->pipefs_sb_lock);
  1114. }
  1115. EXPORT_SYMBOL_GPL(rpc_put_sb_net);
  1116. static int
  1117. rpc_fill_super(struct super_block *sb, void *data, int silent)
  1118. {
  1119. struct inode *inode;
  1120. struct dentry *root;
  1121. struct net *net = data;
  1122. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1123. int err;
  1124. sb->s_blocksize = PAGE_CACHE_SIZE;
  1125. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1126. sb->s_magic = RPCAUTH_GSSMAGIC;
  1127. sb->s_op = &s_ops;
  1128. sb->s_d_op = &simple_dentry_operations;
  1129. sb->s_time_gran = 1;
  1130. inode = rpc_get_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
  1131. sb->s_root = root = d_make_root(inode);
  1132. if (!root)
  1133. return -ENOMEM;
  1134. if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
  1135. return -ENOMEM;
  1136. dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n",
  1137. net, NET_NAME(net));
  1138. mutex_lock(&sn->pipefs_sb_lock);
  1139. sn->pipefs_sb = sb;
  1140. err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1141. RPC_PIPEFS_MOUNT,
  1142. sb);
  1143. if (err)
  1144. goto err_depopulate;
  1145. sb->s_fs_info = get_net(net);
  1146. mutex_unlock(&sn->pipefs_sb_lock);
  1147. return 0;
  1148. err_depopulate:
  1149. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1150. RPC_PIPEFS_UMOUNT,
  1151. sb);
  1152. sn->pipefs_sb = NULL;
  1153. __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
  1154. mutex_unlock(&sn->pipefs_sb_lock);
  1155. return err;
  1156. }
  1157. static struct dentry *
  1158. rpc_mount(struct file_system_type *fs_type,
  1159. int flags, const char *dev_name, void *data)
  1160. {
  1161. return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
  1162. }
  1163. static void rpc_kill_sb(struct super_block *sb)
  1164. {
  1165. struct net *net = sb->s_fs_info;
  1166. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1167. mutex_lock(&sn->pipefs_sb_lock);
  1168. if (sn->pipefs_sb != sb) {
  1169. mutex_unlock(&sn->pipefs_sb_lock);
  1170. goto out;
  1171. }
  1172. sn->pipefs_sb = NULL;
  1173. dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n",
  1174. net, NET_NAME(net));
  1175. blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
  1176. RPC_PIPEFS_UMOUNT,
  1177. sb);
  1178. mutex_unlock(&sn->pipefs_sb_lock);
  1179. put_net(net);
  1180. out:
  1181. kill_litter_super(sb);
  1182. }
  1183. static struct file_system_type rpc_pipe_fs_type = {
  1184. .owner = THIS_MODULE,
  1185. .name = "rpc_pipefs",
  1186. .mount = rpc_mount,
  1187. .kill_sb = rpc_kill_sb,
  1188. };
  1189. MODULE_ALIAS_FS("rpc_pipefs");
  1190. MODULE_ALIAS("rpc_pipefs");
  1191. static void
  1192. init_once(void *foo)
  1193. {
  1194. struct rpc_inode *rpci = (struct rpc_inode *) foo;
  1195. inode_init_once(&rpci->vfs_inode);
  1196. rpci->private = NULL;
  1197. rpci->pipe = NULL;
  1198. init_waitqueue_head(&rpci->waitq);
  1199. }
  1200. int register_rpc_pipefs(void)
  1201. {
  1202. int err;
  1203. rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
  1204. sizeof(struct rpc_inode),
  1205. 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1206. SLAB_MEM_SPREAD),
  1207. init_once);
  1208. if (!rpc_inode_cachep)
  1209. return -ENOMEM;
  1210. err = rpc_clients_notifier_register();
  1211. if (err)
  1212. goto err_notifier;
  1213. err = register_filesystem(&rpc_pipe_fs_type);
  1214. if (err)
  1215. goto err_register;
  1216. return 0;
  1217. err_register:
  1218. rpc_clients_notifier_unregister();
  1219. err_notifier:
  1220. kmem_cache_destroy(rpc_inode_cachep);
  1221. return err;
  1222. }
  1223. void unregister_rpc_pipefs(void)
  1224. {
  1225. rpc_clients_notifier_unregister();
  1226. kmem_cache_destroy(rpc_inode_cachep);
  1227. unregister_filesystem(&rpc_pipe_fs_type);
  1228. }