nfs4recover.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * Copyright (c) 2004 The Regents of the University of Michigan.
  3. * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
  4. * All rights reserved.
  5. *
  6. * Andy Adamson <andros@citi.umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/slab.h>
  36. #include <linux/namei.h>
  37. #include <linux/crypto.h>
  38. #include <linux/sched.h>
  39. #include <linux/fs.h>
  40. #include <linux/module.h>
  41. #include <net/net_namespace.h>
  42. #include <linux/sunrpc/rpc_pipe_fs.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/nfsd/cld.h>
  45. #include "nfsd.h"
  46. #include "state.h"
  47. #include "vfs.h"
  48. #include "netns.h"
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. /* Declarations */
  51. struct nfsd4_client_tracking_ops {
  52. int (*init)(struct net *);
  53. void (*exit)(struct net *);
  54. void (*create)(struct nfs4_client *);
  55. void (*remove)(struct nfs4_client *);
  56. int (*check)(struct nfs4_client *);
  57. void (*grace_done)(struct net *, time_t);
  58. };
  59. /* Globals */
  60. static struct file *rec_file;
  61. static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
  62. static struct nfsd4_client_tracking_ops *client_tracking_ops;
  63. static int
  64. nfs4_save_creds(const struct cred **original_creds)
  65. {
  66. struct cred *new;
  67. new = prepare_creds();
  68. if (!new)
  69. return -ENOMEM;
  70. new->fsuid = 0;
  71. new->fsgid = 0;
  72. *original_creds = override_creds(new);
  73. put_cred(new);
  74. return 0;
  75. }
  76. static void
  77. nfs4_reset_creds(const struct cred *original)
  78. {
  79. revert_creds(original);
  80. }
  81. static void
  82. md5_to_hex(char *out, char *md5)
  83. {
  84. int i;
  85. for (i=0; i<16; i++) {
  86. unsigned char c = md5[i];
  87. *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  88. *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  89. }
  90. *out = '\0';
  91. }
  92. __be32
  93. nfs4_make_rec_clidname(char *dname, struct xdr_netobj *clname)
  94. {
  95. struct xdr_netobj cksum;
  96. struct hash_desc desc;
  97. struct scatterlist sg;
  98. __be32 status = nfserr_jukebox;
  99. dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
  100. clname->len, clname->data);
  101. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  102. desc.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  103. if (IS_ERR(desc.tfm))
  104. goto out_no_tfm;
  105. cksum.len = crypto_hash_digestsize(desc.tfm);
  106. cksum.data = kmalloc(cksum.len, GFP_KERNEL);
  107. if (cksum.data == NULL)
  108. goto out;
  109. sg_init_one(&sg, clname->data, clname->len);
  110. if (crypto_hash_digest(&desc, &sg, sg.length, cksum.data))
  111. goto out;
  112. md5_to_hex(dname, cksum.data);
  113. status = nfs_ok;
  114. out:
  115. kfree(cksum.data);
  116. crypto_free_hash(desc.tfm);
  117. out_no_tfm:
  118. return status;
  119. }
  120. static void
  121. nfsd4_create_clid_dir(struct nfs4_client *clp)
  122. {
  123. const struct cred *original_cred;
  124. char *dname = clp->cl_recdir;
  125. struct dentry *dir, *dentry;
  126. int status;
  127. dprintk("NFSD: nfsd4_create_clid_dir for \"%s\"\n", dname);
  128. if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  129. return;
  130. if (!rec_file)
  131. return;
  132. status = nfs4_save_creds(&original_cred);
  133. if (status < 0)
  134. return;
  135. status = mnt_want_write_file(rec_file);
  136. if (status)
  137. return;
  138. dir = rec_file->f_path.dentry;
  139. /* lock the parent */
  140. mutex_lock(&dir->d_inode->i_mutex);
  141. dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
  142. if (IS_ERR(dentry)) {
  143. status = PTR_ERR(dentry);
  144. goto out_unlock;
  145. }
  146. if (dentry->d_inode)
  147. /*
  148. * In the 4.1 case, where we're called from
  149. * reclaim_complete(), records from the previous reboot
  150. * may still be left, so this is OK.
  151. *
  152. * In the 4.0 case, we should never get here; but we may
  153. * as well be forgiving and just succeed silently.
  154. */
  155. goto out_put;
  156. status = vfs_mkdir(dir->d_inode, dentry, S_IRWXU);
  157. out_put:
  158. dput(dentry);
  159. out_unlock:
  160. mutex_unlock(&dir->d_inode->i_mutex);
  161. if (status == 0)
  162. vfs_fsync(rec_file, 0);
  163. else
  164. printk(KERN_ERR "NFSD: failed to write recovery record"
  165. " (err %d); please check that %s exists"
  166. " and is writeable", status,
  167. user_recovery_dirname);
  168. mnt_drop_write_file(rec_file);
  169. nfs4_reset_creds(original_cred);
  170. }
  171. typedef int (recdir_func)(struct dentry *, struct dentry *);
  172. struct name_list {
  173. char name[HEXDIR_LEN];
  174. struct list_head list;
  175. };
  176. static int
  177. nfsd4_build_namelist(void *arg, const char *name, int namlen,
  178. loff_t offset, u64 ino, unsigned int d_type)
  179. {
  180. struct list_head *names = arg;
  181. struct name_list *entry;
  182. if (namlen != HEXDIR_LEN - 1)
  183. return 0;
  184. entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
  185. if (entry == NULL)
  186. return -ENOMEM;
  187. memcpy(entry->name, name, HEXDIR_LEN - 1);
  188. entry->name[HEXDIR_LEN - 1] = '\0';
  189. list_add(&entry->list, names);
  190. return 0;
  191. }
  192. static int
  193. nfsd4_list_rec_dir(recdir_func *f)
  194. {
  195. const struct cred *original_cred;
  196. struct dentry *dir = rec_file->f_path.dentry;
  197. LIST_HEAD(names);
  198. int status;
  199. status = nfs4_save_creds(&original_cred);
  200. if (status < 0)
  201. return status;
  202. status = vfs_llseek(rec_file, 0, SEEK_SET);
  203. if (status < 0) {
  204. nfs4_reset_creds(original_cred);
  205. return status;
  206. }
  207. status = vfs_readdir(rec_file, nfsd4_build_namelist, &names);
  208. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  209. while (!list_empty(&names)) {
  210. struct name_list *entry;
  211. entry = list_entry(names.next, struct name_list, list);
  212. if (!status) {
  213. struct dentry *dentry;
  214. dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
  215. if (IS_ERR(dentry)) {
  216. status = PTR_ERR(dentry);
  217. break;
  218. }
  219. status = f(dir, dentry);
  220. dput(dentry);
  221. }
  222. list_del(&entry->list);
  223. kfree(entry);
  224. }
  225. mutex_unlock(&dir->d_inode->i_mutex);
  226. nfs4_reset_creds(original_cred);
  227. return status;
  228. }
  229. static int
  230. nfsd4_unlink_clid_dir(char *name, int namlen)
  231. {
  232. struct dentry *dir, *dentry;
  233. int status;
  234. dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
  235. dir = rec_file->f_path.dentry;
  236. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  237. dentry = lookup_one_len(name, dir, namlen);
  238. if (IS_ERR(dentry)) {
  239. status = PTR_ERR(dentry);
  240. goto out_unlock;
  241. }
  242. status = -ENOENT;
  243. if (!dentry->d_inode)
  244. goto out;
  245. status = vfs_rmdir(dir->d_inode, dentry);
  246. out:
  247. dput(dentry);
  248. out_unlock:
  249. mutex_unlock(&dir->d_inode->i_mutex);
  250. return status;
  251. }
  252. static void
  253. nfsd4_remove_clid_dir(struct nfs4_client *clp)
  254. {
  255. const struct cred *original_cred;
  256. int status;
  257. if (!rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  258. return;
  259. status = mnt_want_write_file(rec_file);
  260. if (status)
  261. goto out;
  262. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  263. status = nfs4_save_creds(&original_cred);
  264. if (status < 0)
  265. goto out_drop_write;
  266. status = nfsd4_unlink_clid_dir(clp->cl_recdir, HEXDIR_LEN-1);
  267. nfs4_reset_creds(original_cred);
  268. if (status == 0)
  269. vfs_fsync(rec_file, 0);
  270. out_drop_write:
  271. mnt_drop_write_file(rec_file);
  272. out:
  273. if (status)
  274. printk("NFSD: Failed to remove expired client state directory"
  275. " %.*s\n", HEXDIR_LEN, clp->cl_recdir);
  276. }
  277. static int
  278. purge_old(struct dentry *parent, struct dentry *child)
  279. {
  280. int status;
  281. if (nfs4_has_reclaimed_state(child->d_name.name))
  282. return 0;
  283. status = vfs_rmdir(parent->d_inode, child);
  284. if (status)
  285. printk("failed to remove client recovery directory %s\n",
  286. child->d_name.name);
  287. /* Keep trying, success or failure: */
  288. return 0;
  289. }
  290. static void
  291. nfsd4_recdir_purge_old(struct net *net, time_t boot_time)
  292. {
  293. int status;
  294. if (!rec_file)
  295. return;
  296. status = mnt_want_write_file(rec_file);
  297. if (status)
  298. goto out;
  299. status = nfsd4_list_rec_dir(purge_old);
  300. if (status == 0)
  301. vfs_fsync(rec_file, 0);
  302. mnt_drop_write_file(rec_file);
  303. out:
  304. if (status)
  305. printk("nfsd4: failed to purge old clients from recovery"
  306. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  307. }
  308. static int
  309. load_recdir(struct dentry *parent, struct dentry *child)
  310. {
  311. if (child->d_name.len != HEXDIR_LEN - 1) {
  312. printk("nfsd4: illegal name %s in recovery directory\n",
  313. child->d_name.name);
  314. /* Keep trying; maybe the others are OK: */
  315. return 0;
  316. }
  317. nfs4_client_to_reclaim(child->d_name.name);
  318. return 0;
  319. }
  320. static int
  321. nfsd4_recdir_load(void) {
  322. int status;
  323. if (!rec_file)
  324. return 0;
  325. status = nfsd4_list_rec_dir(load_recdir);
  326. if (status)
  327. printk("nfsd4: failed loading clients from recovery"
  328. " directory %s\n", rec_file->f_path.dentry->d_name.name);
  329. return status;
  330. }
  331. /*
  332. * Hold reference to the recovery directory.
  333. */
  334. static int
  335. nfsd4_init_recdir(void)
  336. {
  337. const struct cred *original_cred;
  338. int status;
  339. printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
  340. user_recovery_dirname);
  341. BUG_ON(rec_file);
  342. status = nfs4_save_creds(&original_cred);
  343. if (status < 0) {
  344. printk("NFSD: Unable to change credentials to find recovery"
  345. " directory: error %d\n",
  346. status);
  347. return status;
  348. }
  349. rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
  350. if (IS_ERR(rec_file)) {
  351. printk("NFSD: unable to find recovery directory %s\n",
  352. user_recovery_dirname);
  353. status = PTR_ERR(rec_file);
  354. rec_file = NULL;
  355. }
  356. nfs4_reset_creds(original_cred);
  357. return status;
  358. }
  359. static int
  360. nfsd4_load_reboot_recovery_data(struct net *net)
  361. {
  362. int status;
  363. /* XXX: The legacy code won't work in a container */
  364. if (net != &init_net) {
  365. WARN(1, KERN_ERR "NFSD: attempt to initialize legacy client "
  366. "tracking in a container!\n");
  367. return -EINVAL;
  368. }
  369. nfs4_lock_state();
  370. status = nfsd4_init_recdir();
  371. if (!status)
  372. status = nfsd4_recdir_load();
  373. nfs4_unlock_state();
  374. if (status)
  375. printk(KERN_ERR "NFSD: Failure reading reboot recovery data\n");
  376. return status;
  377. }
  378. static void
  379. nfsd4_shutdown_recdir(void)
  380. {
  381. if (!rec_file)
  382. return;
  383. fput(rec_file);
  384. rec_file = NULL;
  385. }
  386. static void
  387. nfsd4_legacy_tracking_exit(struct net *net)
  388. {
  389. nfs4_release_reclaim();
  390. nfsd4_shutdown_recdir();
  391. }
  392. /*
  393. * Change the NFSv4 recovery directory to recdir.
  394. */
  395. int
  396. nfs4_reset_recoverydir(char *recdir)
  397. {
  398. int status;
  399. struct path path;
  400. status = kern_path(recdir, LOOKUP_FOLLOW, &path);
  401. if (status)
  402. return status;
  403. status = -ENOTDIR;
  404. if (S_ISDIR(path.dentry->d_inode->i_mode)) {
  405. strcpy(user_recovery_dirname, recdir);
  406. status = 0;
  407. }
  408. path_put(&path);
  409. return status;
  410. }
  411. char *
  412. nfs4_recoverydir(void)
  413. {
  414. return user_recovery_dirname;
  415. }
  416. static int
  417. nfsd4_check_legacy_client(struct nfs4_client *clp)
  418. {
  419. /* did we already find that this client is stable? */
  420. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  421. return 0;
  422. /* look for it in the reclaim hashtable otherwise */
  423. if (nfsd4_find_reclaim_client(clp)) {
  424. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  425. return 0;
  426. }
  427. return -ENOENT;
  428. }
  429. static struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
  430. .init = nfsd4_load_reboot_recovery_data,
  431. .exit = nfsd4_legacy_tracking_exit,
  432. .create = nfsd4_create_clid_dir,
  433. .remove = nfsd4_remove_clid_dir,
  434. .check = nfsd4_check_legacy_client,
  435. .grace_done = nfsd4_recdir_purge_old,
  436. };
  437. /* Globals */
  438. #define NFSD_PIPE_DIR "nfsd"
  439. #define NFSD_CLD_PIPE "cld"
  440. /* per-net-ns structure for holding cld upcall info */
  441. struct cld_net {
  442. struct rpc_pipe *cn_pipe;
  443. spinlock_t cn_lock;
  444. struct list_head cn_list;
  445. unsigned int cn_xid;
  446. };
  447. struct cld_upcall {
  448. struct list_head cu_list;
  449. struct cld_net *cu_net;
  450. struct task_struct *cu_task;
  451. struct cld_msg cu_msg;
  452. };
  453. static int
  454. __cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  455. {
  456. int ret;
  457. struct rpc_pipe_msg msg;
  458. memset(&msg, 0, sizeof(msg));
  459. msg.data = cmsg;
  460. msg.len = sizeof(*cmsg);
  461. /*
  462. * Set task state before we queue the upcall. That prevents
  463. * wake_up_process in the downcall from racing with schedule.
  464. */
  465. set_current_state(TASK_UNINTERRUPTIBLE);
  466. ret = rpc_queue_upcall(pipe, &msg);
  467. if (ret < 0) {
  468. set_current_state(TASK_RUNNING);
  469. goto out;
  470. }
  471. schedule();
  472. set_current_state(TASK_RUNNING);
  473. if (msg.errno < 0)
  474. ret = msg.errno;
  475. out:
  476. return ret;
  477. }
  478. static int
  479. cld_pipe_upcall(struct rpc_pipe *pipe, struct cld_msg *cmsg)
  480. {
  481. int ret;
  482. /*
  483. * -EAGAIN occurs when pipe is closed and reopened while there are
  484. * upcalls queued.
  485. */
  486. do {
  487. ret = __cld_pipe_upcall(pipe, cmsg);
  488. } while (ret == -EAGAIN);
  489. return ret;
  490. }
  491. static ssize_t
  492. cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  493. {
  494. struct cld_upcall *tmp, *cup;
  495. struct cld_msg __user *cmsg = (struct cld_msg __user *)src;
  496. uint32_t xid;
  497. struct nfsd_net *nn = net_generic(filp->f_dentry->d_sb->s_fs_info,
  498. nfsd_net_id);
  499. struct cld_net *cn = nn->cld_net;
  500. if (mlen != sizeof(*cmsg)) {
  501. dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
  502. sizeof(*cmsg));
  503. return -EINVAL;
  504. }
  505. /* copy just the xid so we can try to find that */
  506. if (copy_from_user(&xid, &cmsg->cm_xid, sizeof(xid)) != 0) {
  507. dprintk("%s: error when copying xid from userspace", __func__);
  508. return -EFAULT;
  509. }
  510. /* walk the list and find corresponding xid */
  511. cup = NULL;
  512. spin_lock(&cn->cn_lock);
  513. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  514. if (get_unaligned(&tmp->cu_msg.cm_xid) == xid) {
  515. cup = tmp;
  516. list_del_init(&cup->cu_list);
  517. break;
  518. }
  519. }
  520. spin_unlock(&cn->cn_lock);
  521. /* couldn't find upcall? */
  522. if (!cup) {
  523. dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
  524. return -EINVAL;
  525. }
  526. if (copy_from_user(&cup->cu_msg, src, mlen) != 0)
  527. return -EFAULT;
  528. wake_up_process(cup->cu_task);
  529. return mlen;
  530. }
  531. static void
  532. cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  533. {
  534. struct cld_msg *cmsg = msg->data;
  535. struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
  536. cu_msg);
  537. /* errno >= 0 means we got a downcall */
  538. if (msg->errno >= 0)
  539. return;
  540. wake_up_process(cup->cu_task);
  541. }
  542. static const struct rpc_pipe_ops cld_upcall_ops = {
  543. .upcall = rpc_pipe_generic_upcall,
  544. .downcall = cld_pipe_downcall,
  545. .destroy_msg = cld_pipe_destroy_msg,
  546. };
  547. static struct dentry *
  548. nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
  549. {
  550. struct dentry *dir, *dentry;
  551. dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
  552. if (dir == NULL)
  553. return ERR_PTR(-ENOENT);
  554. dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
  555. dput(dir);
  556. return dentry;
  557. }
  558. static void
  559. nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
  560. {
  561. if (pipe->dentry)
  562. rpc_unlink(pipe->dentry);
  563. }
  564. static struct dentry *
  565. nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
  566. {
  567. struct super_block *sb;
  568. struct dentry *dentry;
  569. sb = rpc_get_sb_net(net);
  570. if (!sb)
  571. return NULL;
  572. dentry = nfsd4_cld_register_sb(sb, pipe);
  573. rpc_put_sb_net(net);
  574. return dentry;
  575. }
  576. static void
  577. nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
  578. {
  579. struct super_block *sb;
  580. sb = rpc_get_sb_net(net);
  581. if (sb) {
  582. nfsd4_cld_unregister_sb(pipe);
  583. rpc_put_sb_net(net);
  584. }
  585. }
  586. /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
  587. static int
  588. nfsd4_init_cld_pipe(struct net *net)
  589. {
  590. int ret;
  591. struct dentry *dentry;
  592. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  593. struct cld_net *cn;
  594. if (nn->cld_net)
  595. return 0;
  596. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  597. if (!cn) {
  598. ret = -ENOMEM;
  599. goto err;
  600. }
  601. cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
  602. if (IS_ERR(cn->cn_pipe)) {
  603. ret = PTR_ERR(cn->cn_pipe);
  604. goto err;
  605. }
  606. spin_lock_init(&cn->cn_lock);
  607. INIT_LIST_HEAD(&cn->cn_list);
  608. dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
  609. if (IS_ERR(dentry)) {
  610. ret = PTR_ERR(dentry);
  611. goto err_destroy_data;
  612. }
  613. cn->cn_pipe->dentry = dentry;
  614. nn->cld_net = cn;
  615. return 0;
  616. err_destroy_data:
  617. rpc_destroy_pipe_data(cn->cn_pipe);
  618. err:
  619. kfree(cn);
  620. printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
  621. ret);
  622. return ret;
  623. }
  624. static void
  625. nfsd4_remove_cld_pipe(struct net *net)
  626. {
  627. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  628. struct cld_net *cn = nn->cld_net;
  629. nfsd4_cld_unregister_net(net, cn->cn_pipe);
  630. rpc_destroy_pipe_data(cn->cn_pipe);
  631. kfree(nn->cld_net);
  632. nn->cld_net = NULL;
  633. }
  634. static struct cld_upcall *
  635. alloc_cld_upcall(struct cld_net *cn)
  636. {
  637. struct cld_upcall *new, *tmp;
  638. new = kzalloc(sizeof(*new), GFP_KERNEL);
  639. if (!new)
  640. return new;
  641. /* FIXME: hard cap on number in flight? */
  642. restart_search:
  643. spin_lock(&cn->cn_lock);
  644. list_for_each_entry(tmp, &cn->cn_list, cu_list) {
  645. if (tmp->cu_msg.cm_xid == cn->cn_xid) {
  646. cn->cn_xid++;
  647. spin_unlock(&cn->cn_lock);
  648. goto restart_search;
  649. }
  650. }
  651. new->cu_task = current;
  652. new->cu_msg.cm_vers = CLD_UPCALL_VERSION;
  653. put_unaligned(cn->cn_xid++, &new->cu_msg.cm_xid);
  654. new->cu_net = cn;
  655. list_add(&new->cu_list, &cn->cn_list);
  656. spin_unlock(&cn->cn_lock);
  657. dprintk("%s: allocated xid %u\n", __func__, new->cu_msg.cm_xid);
  658. return new;
  659. }
  660. static void
  661. free_cld_upcall(struct cld_upcall *victim)
  662. {
  663. struct cld_net *cn = victim->cu_net;
  664. spin_lock(&cn->cn_lock);
  665. list_del(&victim->cu_list);
  666. spin_unlock(&cn->cn_lock);
  667. kfree(victim);
  668. }
  669. /* Ask daemon to create a new record */
  670. static void
  671. nfsd4_cld_create(struct nfs4_client *clp)
  672. {
  673. int ret;
  674. struct cld_upcall *cup;
  675. /* FIXME: determine net from clp */
  676. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  677. struct cld_net *cn = nn->cld_net;
  678. /* Don't upcall if it's already stored */
  679. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  680. return;
  681. cup = alloc_cld_upcall(cn);
  682. if (!cup) {
  683. ret = -ENOMEM;
  684. goto out_err;
  685. }
  686. cup->cu_msg.cm_cmd = Cld_Create;
  687. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  688. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  689. clp->cl_name.len);
  690. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  691. if (!ret) {
  692. ret = cup->cu_msg.cm_status;
  693. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  694. }
  695. free_cld_upcall(cup);
  696. out_err:
  697. if (ret)
  698. printk(KERN_ERR "NFSD: Unable to create client "
  699. "record on stable storage: %d\n", ret);
  700. }
  701. /* Ask daemon to create a new record */
  702. static void
  703. nfsd4_cld_remove(struct nfs4_client *clp)
  704. {
  705. int ret;
  706. struct cld_upcall *cup;
  707. /* FIXME: determine net from clp */
  708. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  709. struct cld_net *cn = nn->cld_net;
  710. /* Don't upcall if it's already removed */
  711. if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  712. return;
  713. cup = alloc_cld_upcall(cn);
  714. if (!cup) {
  715. ret = -ENOMEM;
  716. goto out_err;
  717. }
  718. cup->cu_msg.cm_cmd = Cld_Remove;
  719. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  720. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  721. clp->cl_name.len);
  722. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  723. if (!ret) {
  724. ret = cup->cu_msg.cm_status;
  725. clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  726. }
  727. free_cld_upcall(cup);
  728. out_err:
  729. if (ret)
  730. printk(KERN_ERR "NFSD: Unable to remove client "
  731. "record from stable storage: %d\n", ret);
  732. }
  733. /* Check for presence of a record, and update its timestamp */
  734. static int
  735. nfsd4_cld_check(struct nfs4_client *clp)
  736. {
  737. int ret;
  738. struct cld_upcall *cup;
  739. /* FIXME: determine net from clp */
  740. struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
  741. struct cld_net *cn = nn->cld_net;
  742. /* Don't upcall if one was already stored during this grace pd */
  743. if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
  744. return 0;
  745. cup = alloc_cld_upcall(cn);
  746. if (!cup) {
  747. printk(KERN_ERR "NFSD: Unable to check client record on "
  748. "stable storage: %d\n", -ENOMEM);
  749. return -ENOMEM;
  750. }
  751. cup->cu_msg.cm_cmd = Cld_Check;
  752. cup->cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
  753. memcpy(cup->cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
  754. clp->cl_name.len);
  755. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  756. if (!ret) {
  757. ret = cup->cu_msg.cm_status;
  758. set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  759. }
  760. free_cld_upcall(cup);
  761. return ret;
  762. }
  763. static void
  764. nfsd4_cld_grace_done(struct net *net, time_t boot_time)
  765. {
  766. int ret;
  767. struct cld_upcall *cup;
  768. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  769. struct cld_net *cn = nn->cld_net;
  770. cup = alloc_cld_upcall(cn);
  771. if (!cup) {
  772. ret = -ENOMEM;
  773. goto out_err;
  774. }
  775. cup->cu_msg.cm_cmd = Cld_GraceDone;
  776. cup->cu_msg.cm_u.cm_gracetime = (int64_t)boot_time;
  777. ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_msg);
  778. if (!ret)
  779. ret = cup->cu_msg.cm_status;
  780. free_cld_upcall(cup);
  781. out_err:
  782. if (ret)
  783. printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
  784. }
  785. static struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
  786. .init = nfsd4_init_cld_pipe,
  787. .exit = nfsd4_remove_cld_pipe,
  788. .create = nfsd4_cld_create,
  789. .remove = nfsd4_cld_remove,
  790. .check = nfsd4_cld_check,
  791. .grace_done = nfsd4_cld_grace_done,
  792. };
  793. /* upcall via usermodehelper */
  794. static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
  795. module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
  796. S_IRUGO|S_IWUSR);
  797. MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
  798. static int
  799. nfsd4_umh_cltrack_upcall(char *cmd, char *arg)
  800. {
  801. char *envp[] = { NULL };
  802. char *argv[4];
  803. int ret;
  804. if (unlikely(!cltrack_prog[0])) {
  805. dprintk("%s: cltrack_prog is disabled\n", __func__);
  806. return -EACCES;
  807. }
  808. dprintk("%s: cmd: %s\n", __func__, cmd);
  809. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  810. argv[0] = (char *)cltrack_prog;
  811. argv[1] = cmd;
  812. argv[2] = arg;
  813. argv[3] = NULL;
  814. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  815. /*
  816. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  817. * error. The admin can re-enable it on the fly by using sysfs
  818. * once the problem has been fixed.
  819. */
  820. if (ret == -ENOENT || ret == -EACCES) {
  821. dprintk("NFSD: %s was not found or isn't executable (%d). "
  822. "Setting cltrack_prog to blank string!",
  823. cltrack_prog, ret);
  824. cltrack_prog[0] = '\0';
  825. }
  826. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  827. return ret;
  828. }
  829. static char *
  830. bin_to_hex_dup(const unsigned char *src, int srclen)
  831. {
  832. int i;
  833. char *buf, *hex;
  834. /* +1 for terminating NULL */
  835. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  836. if (!buf)
  837. return buf;
  838. hex = buf;
  839. for (i = 0; i < srclen; i++) {
  840. sprintf(hex, "%2.2x", *src++);
  841. hex += 2;
  842. }
  843. return buf;
  844. }
  845. static int
  846. nfsd4_umh_cltrack_init(struct net __attribute__((unused)) *net)
  847. {
  848. return nfsd4_umh_cltrack_upcall("init", NULL);
  849. }
  850. static void
  851. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  852. {
  853. char *hexid;
  854. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  855. if (!hexid) {
  856. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  857. return;
  858. }
  859. nfsd4_umh_cltrack_upcall("create", hexid);
  860. kfree(hexid);
  861. }
  862. static void
  863. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  864. {
  865. char *hexid;
  866. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  867. if (!hexid) {
  868. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  869. return;
  870. }
  871. nfsd4_umh_cltrack_upcall("remove", hexid);
  872. kfree(hexid);
  873. }
  874. static int
  875. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  876. {
  877. int ret;
  878. char *hexid;
  879. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  880. if (!hexid) {
  881. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  882. return -ENOMEM;
  883. }
  884. ret = nfsd4_umh_cltrack_upcall("check", hexid);
  885. kfree(hexid);
  886. return ret;
  887. }
  888. static void
  889. nfsd4_umh_cltrack_grace_done(struct net __attribute__((unused)) *net,
  890. time_t boot_time)
  891. {
  892. char timestr[22]; /* FIXME: better way to determine max size? */
  893. sprintf(timestr, "%ld", boot_time);
  894. nfsd4_umh_cltrack_upcall("gracedone", timestr);
  895. }
  896. static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  897. .init = nfsd4_umh_cltrack_init,
  898. .exit = NULL,
  899. .create = nfsd4_umh_cltrack_create,
  900. .remove = nfsd4_umh_cltrack_remove,
  901. .check = nfsd4_umh_cltrack_check,
  902. .grace_done = nfsd4_umh_cltrack_grace_done,
  903. };
  904. int
  905. nfsd4_client_tracking_init(struct net *net)
  906. {
  907. int status;
  908. struct path path;
  909. /* just run the init if it the method is already decided */
  910. if (client_tracking_ops)
  911. goto do_init;
  912. /*
  913. * First, try a UMH upcall. It should succeed or fail quickly, so
  914. * there's little harm in trying that first.
  915. */
  916. client_tracking_ops = &nfsd4_umh_tracking_ops;
  917. status = client_tracking_ops->init(net);
  918. if (!status)
  919. return status;
  920. /*
  921. * See if the recoverydir exists and is a directory. If it is,
  922. * then use the legacy ops.
  923. */
  924. client_tracking_ops = &nfsd4_legacy_tracking_ops;
  925. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  926. if (!status) {
  927. status = S_ISDIR(path.dentry->d_inode->i_mode);
  928. path_put(&path);
  929. if (status)
  930. goto do_init;
  931. }
  932. /* Finally, try to use nfsdcld */
  933. client_tracking_ops = &nfsd4_cld_tracking_ops;
  934. do_init:
  935. status = client_tracking_ops->init(net);
  936. if (status) {
  937. printk(KERN_WARNING "NFSD: Unable to initialize client "
  938. "recovery tracking! (%d)\n", status);
  939. client_tracking_ops = NULL;
  940. }
  941. return status;
  942. }
  943. void
  944. nfsd4_client_tracking_exit(struct net *net)
  945. {
  946. if (client_tracking_ops) {
  947. if (client_tracking_ops->exit)
  948. client_tracking_ops->exit(net);
  949. client_tracking_ops = NULL;
  950. }
  951. }
  952. void
  953. nfsd4_client_record_create(struct nfs4_client *clp)
  954. {
  955. if (client_tracking_ops)
  956. client_tracking_ops->create(clp);
  957. }
  958. void
  959. nfsd4_client_record_remove(struct nfs4_client *clp)
  960. {
  961. if (client_tracking_ops)
  962. client_tracking_ops->remove(clp);
  963. }
  964. int
  965. nfsd4_client_record_check(struct nfs4_client *clp)
  966. {
  967. if (client_tracking_ops)
  968. return client_tracking_ops->check(clp);
  969. return -EOPNOTSUPP;
  970. }
  971. void
  972. nfsd4_record_grace_done(struct net *net, time_t boot_time)
  973. {
  974. if (client_tracking_ops)
  975. client_tracking_ops->grace_done(net, boot_time);
  976. }
  977. static int
  978. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  979. {
  980. struct super_block *sb = ptr;
  981. struct net *net = sb->s_fs_info;
  982. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  983. struct cld_net *cn = nn->cld_net;
  984. struct dentry *dentry;
  985. int ret = 0;
  986. if (!try_module_get(THIS_MODULE))
  987. return 0;
  988. if (!cn) {
  989. module_put(THIS_MODULE);
  990. return 0;
  991. }
  992. switch (event) {
  993. case RPC_PIPEFS_MOUNT:
  994. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  995. if (IS_ERR(dentry)) {
  996. ret = PTR_ERR(dentry);
  997. break;
  998. }
  999. cn->cn_pipe->dentry = dentry;
  1000. break;
  1001. case RPC_PIPEFS_UMOUNT:
  1002. if (cn->cn_pipe->dentry)
  1003. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1004. break;
  1005. default:
  1006. ret = -ENOTSUPP;
  1007. break;
  1008. }
  1009. module_put(THIS_MODULE);
  1010. return ret;
  1011. }
  1012. static struct notifier_block nfsd4_cld_block = {
  1013. .notifier_call = rpc_pipefs_event,
  1014. };
  1015. int
  1016. register_cld_notifier(void)
  1017. {
  1018. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1019. }
  1020. void
  1021. unregister_cld_notifier(void)
  1022. {
  1023. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1024. }