nfs4recover.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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 bool cltrack_legacy_disable;
  799. module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
  800. MODULE_PARM_DESC(cltrack_legacy_disable,
  801. "Disable legacy recoverydir conversion. Default: false");
  802. #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
  803. #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
  804. static char *
  805. nfsd4_cltrack_legacy_topdir(void)
  806. {
  807. int copied;
  808. size_t len;
  809. char *result;
  810. if (cltrack_legacy_disable)
  811. return NULL;
  812. len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
  813. strlen(nfs4_recoverydir()) + 1;
  814. result = kmalloc(len, GFP_KERNEL);
  815. if (!result)
  816. return result;
  817. copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
  818. nfs4_recoverydir());
  819. if (copied >= len) {
  820. /* just return nothing if output was truncated */
  821. kfree(result);
  822. return NULL;
  823. }
  824. return result;
  825. }
  826. static char *
  827. nfsd4_cltrack_legacy_recdir(const char *recdir)
  828. {
  829. int copied;
  830. size_t len;
  831. char *result;
  832. if (cltrack_legacy_disable)
  833. return NULL;
  834. /* +1 is for '/' between "topdir" and "recdir" */
  835. len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
  836. strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
  837. result = kmalloc(len, GFP_KERNEL);
  838. if (!result)
  839. return result;
  840. copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/%s",
  841. nfs4_recoverydir(), recdir);
  842. if (copied >= len) {
  843. /* just return nothing if output was truncated */
  844. kfree(result);
  845. return NULL;
  846. }
  847. return result;
  848. }
  849. static int
  850. nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *legacy)
  851. {
  852. char *envp[2];
  853. char *argv[4];
  854. int ret;
  855. if (unlikely(!cltrack_prog[0])) {
  856. dprintk("%s: cltrack_prog is disabled\n", __func__);
  857. return -EACCES;
  858. }
  859. dprintk("%s: cmd: %s\n", __func__, cmd);
  860. dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
  861. dprintk("%s: legacy: %s\n", __func__, legacy ? legacy : "(null)");
  862. envp[0] = legacy;
  863. envp[1] = NULL;
  864. argv[0] = (char *)cltrack_prog;
  865. argv[1] = cmd;
  866. argv[2] = arg;
  867. argv[3] = NULL;
  868. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
  869. /*
  870. * Disable the upcall mechanism if we're getting an ENOENT or EACCES
  871. * error. The admin can re-enable it on the fly by using sysfs
  872. * once the problem has been fixed.
  873. */
  874. if (ret == -ENOENT || ret == -EACCES) {
  875. dprintk("NFSD: %s was not found or isn't executable (%d). "
  876. "Setting cltrack_prog to blank string!",
  877. cltrack_prog, ret);
  878. cltrack_prog[0] = '\0';
  879. }
  880. dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
  881. return ret;
  882. }
  883. static char *
  884. bin_to_hex_dup(const unsigned char *src, int srclen)
  885. {
  886. int i;
  887. char *buf, *hex;
  888. /* +1 for terminating NULL */
  889. buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
  890. if (!buf)
  891. return buf;
  892. hex = buf;
  893. for (i = 0; i < srclen; i++) {
  894. sprintf(hex, "%2.2x", *src++);
  895. hex += 2;
  896. }
  897. return buf;
  898. }
  899. static int
  900. nfsd4_umh_cltrack_init(struct net __attribute__((unused)) *net)
  901. {
  902. return nfsd4_umh_cltrack_upcall("init", NULL, NULL);
  903. }
  904. static void
  905. nfsd4_umh_cltrack_create(struct nfs4_client *clp)
  906. {
  907. char *hexid;
  908. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  909. if (!hexid) {
  910. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  911. return;
  912. }
  913. nfsd4_umh_cltrack_upcall("create", hexid, NULL);
  914. kfree(hexid);
  915. }
  916. static void
  917. nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
  918. {
  919. char *hexid;
  920. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  921. if (!hexid) {
  922. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  923. return;
  924. }
  925. nfsd4_umh_cltrack_upcall("remove", hexid, NULL);
  926. kfree(hexid);
  927. }
  928. static int
  929. nfsd4_umh_cltrack_check(struct nfs4_client *clp)
  930. {
  931. int ret;
  932. char *hexid, *legacy;
  933. hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
  934. if (!hexid) {
  935. dprintk("%s: can't allocate memory for upcall!\n", __func__);
  936. return -ENOMEM;
  937. }
  938. legacy = nfsd4_cltrack_legacy_recdir(clp->cl_recdir);
  939. ret = nfsd4_umh_cltrack_upcall("check", hexid, legacy);
  940. kfree(legacy);
  941. kfree(hexid);
  942. return ret;
  943. }
  944. static void
  945. nfsd4_umh_cltrack_grace_done(struct net __attribute__((unused)) *net,
  946. time_t boot_time)
  947. {
  948. char *legacy;
  949. char timestr[22]; /* FIXME: better way to determine max size? */
  950. sprintf(timestr, "%ld", boot_time);
  951. legacy = nfsd4_cltrack_legacy_topdir();
  952. nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy);
  953. kfree(legacy);
  954. }
  955. static struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
  956. .init = nfsd4_umh_cltrack_init,
  957. .exit = NULL,
  958. .create = nfsd4_umh_cltrack_create,
  959. .remove = nfsd4_umh_cltrack_remove,
  960. .check = nfsd4_umh_cltrack_check,
  961. .grace_done = nfsd4_umh_cltrack_grace_done,
  962. };
  963. int
  964. nfsd4_client_tracking_init(struct net *net)
  965. {
  966. int status;
  967. struct path path;
  968. /* just run the init if it the method is already decided */
  969. if (client_tracking_ops)
  970. goto do_init;
  971. /*
  972. * First, try a UMH upcall. It should succeed or fail quickly, so
  973. * there's little harm in trying that first.
  974. */
  975. client_tracking_ops = &nfsd4_umh_tracking_ops;
  976. status = client_tracking_ops->init(net);
  977. if (!status)
  978. return status;
  979. /*
  980. * See if the recoverydir exists and is a directory. If it is,
  981. * then use the legacy ops.
  982. */
  983. client_tracking_ops = &nfsd4_legacy_tracking_ops;
  984. status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
  985. if (!status) {
  986. status = S_ISDIR(path.dentry->d_inode->i_mode);
  987. path_put(&path);
  988. if (status)
  989. goto do_init;
  990. }
  991. /* Finally, try to use nfsdcld */
  992. client_tracking_ops = &nfsd4_cld_tracking_ops;
  993. do_init:
  994. status = client_tracking_ops->init(net);
  995. if (status) {
  996. printk(KERN_WARNING "NFSD: Unable to initialize client "
  997. "recovery tracking! (%d)\n", status);
  998. client_tracking_ops = NULL;
  999. }
  1000. return status;
  1001. }
  1002. void
  1003. nfsd4_client_tracking_exit(struct net *net)
  1004. {
  1005. if (client_tracking_ops) {
  1006. if (client_tracking_ops->exit)
  1007. client_tracking_ops->exit(net);
  1008. client_tracking_ops = NULL;
  1009. }
  1010. }
  1011. void
  1012. nfsd4_client_record_create(struct nfs4_client *clp)
  1013. {
  1014. if (client_tracking_ops)
  1015. client_tracking_ops->create(clp);
  1016. }
  1017. void
  1018. nfsd4_client_record_remove(struct nfs4_client *clp)
  1019. {
  1020. if (client_tracking_ops)
  1021. client_tracking_ops->remove(clp);
  1022. }
  1023. int
  1024. nfsd4_client_record_check(struct nfs4_client *clp)
  1025. {
  1026. if (client_tracking_ops)
  1027. return client_tracking_ops->check(clp);
  1028. return -EOPNOTSUPP;
  1029. }
  1030. void
  1031. nfsd4_record_grace_done(struct net *net, time_t boot_time)
  1032. {
  1033. if (client_tracking_ops)
  1034. client_tracking_ops->grace_done(net, boot_time);
  1035. }
  1036. static int
  1037. rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
  1038. {
  1039. struct super_block *sb = ptr;
  1040. struct net *net = sb->s_fs_info;
  1041. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1042. struct cld_net *cn = nn->cld_net;
  1043. struct dentry *dentry;
  1044. int ret = 0;
  1045. if (!try_module_get(THIS_MODULE))
  1046. return 0;
  1047. if (!cn) {
  1048. module_put(THIS_MODULE);
  1049. return 0;
  1050. }
  1051. switch (event) {
  1052. case RPC_PIPEFS_MOUNT:
  1053. dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
  1054. if (IS_ERR(dentry)) {
  1055. ret = PTR_ERR(dentry);
  1056. break;
  1057. }
  1058. cn->cn_pipe->dentry = dentry;
  1059. break;
  1060. case RPC_PIPEFS_UMOUNT:
  1061. if (cn->cn_pipe->dentry)
  1062. nfsd4_cld_unregister_sb(cn->cn_pipe);
  1063. break;
  1064. default:
  1065. ret = -ENOTSUPP;
  1066. break;
  1067. }
  1068. module_put(THIS_MODULE);
  1069. return ret;
  1070. }
  1071. static struct notifier_block nfsd4_cld_block = {
  1072. .notifier_call = rpc_pipefs_event,
  1073. };
  1074. int
  1075. register_cld_notifier(void)
  1076. {
  1077. return rpc_pipefs_notifier_register(&nfsd4_cld_block);
  1078. }
  1079. void
  1080. unregister_cld_notifier(void)
  1081. {
  1082. rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
  1083. }